Definition
- Even – N is exactly divisible by 2, meaning
N % 2 == 0. - Odd – N is not exactly divisible by 2, meaning
N % 2 != 0. - Negative sign rule – Java's
%keeps the dividend's sign:-7 % 2is-1. - Robust odd test –
N % 2 != 0works for positive, negative, and zero. - Bitwise alternative –
(N & 1) != 0checks the lowest bit; equivalent but less clear.
National Bank of India routes transactions to one of two processing queues: even-numbered IDs to queue A and odd ones to queue B. Rajesh's transaction with ID 7 is checked: 7 % 2 is 1, not zero, so it is odd and goes to queue B.
Understanding the Concepts
What is the algorithm?
Read N, compute N % 2. If the result is 0, print Even. Otherwise print Odd. That is the entire logic.
package com.bank.service;
public class ParityRouter {
public static String classify(int transactionId) {
return (transactionId % 2 == 0) ? "Even" : "Odd";
}
public static void main(String[] args) {
int[] ids = {7, 10, -5, 0, 42};
for (int id : ids) {
System.out.println(id + " -> " + classify(id));
}
}
}
7 -> Odd
10 -> Even
-5 -> Odd
0 -> Even
42 -> Even
Why is N % 2 == 1 wrong for negative numbers?
In Java the remainder takes the sign of the dividend. When the dividend is negative, the remainder is also negative. This means -5 % 2 produces -1, not 1. A test like N % 2 == 1 returns false for -5 even though -5 is odd.
System.out.println(-5 % 2); // -1 (not 1)
System.out.println(-5 % 2 == 1); // false (WRONG: -5 is odd)
System.out.println(-5 % 2 != 0); // true (CORRECT)
What is the difference between % and Math.floorMod?
Java provides Math.floorMod(a, b) which always returns a non-negative result when the divisor is positive. For parity, Math.floorMod(-5, 2) returns 1, not -1. This means Math.floorMod(n, 2) == 1 correctly identifies negative odds. However, the != 0 approach with the standard % operator is simpler and widely understood.
System.out.println(Math.floorMod(-5, 2)); // 1 (always non-negative)
System.out.println(-5 % 2); // -1 (sign of dividend)
How is modulo different from bitwise AND?
| Feature | N % 2 | N & 1 |
|---|---|---|
| Mechanism | division remainder | lowest bit |
| Negative result | can be -1 | always 0 or 1 |
| Readability | clear | requires bit knowledge |
| Odd test | != 0 | != 0 |
Both correctly detect odd with != 0. The compiler optimizes % 2 to a bit operation anyway.
How does parity routing work in banking?
At National Bank of India, transaction IDs are routed to one of two processing queues based on parity. This distributes load roughly evenly without any lookup table or hash function.
package com.bank.service;
public class TransactionRouter {
public static String routeToQueue(int transactionId) {
if (transactionId % 2 == 0) {
return "QUEUE_A";
}
return "QUEUE_B";
}
public static void main(String[] args) {
int[] txnIds = {1001, 1002, 1003, 1004};
for (int id : txnIds) {
System.out.println("Txn " + id + " -> " + routeToQueue(id));
}
}
}
Txn 1001 -> QUEUE_B
Txn 1002 -> QUEUE_A
Txn 1003 -> QUEUE_B
Txn 1004 -> QUEUE_A
What are edge cases?
- Zero: even, because 0 % 2 == 0.
- Integer.MIN_VALUE: even (it is divisible by 2).
- Large positive and negative values: modulo works correctly.
What are common mistakes?
// Wrong: == 1 misses negative odds
if (n % 2 == 1) { /* misses -1, -3, -5, ... */ }
// Correct: != 0 catches all odds
if (n % 2 != 0) { /* works for -1, -3, 1, 3, ... */ }
What about BigDecimal parity?
For BigDecimal, convert to BigInteger and use testBit(0) to check the lowest bit. The testBit(0) method returns true when the lowest bit is 1, meaning the number is odd.
import java.math.BigDecimal;
import java.math.BigInteger;
BigDecimal amount = new BigDecimal("7");
boolean isOdd = amount.toBigInteger().testBit(0); // true
boolean isEven = !isOdd; // false
BigDecimal fee = new BigDecimal("42");
System.out.println(fee.toBigInteger().testBit(0)); // false -> even
What should a senior engineer know?
Parity partitioning is a simple but effective load-balancing strategy. At National Bank of India, even-numbered transaction IDs went to processing lane A and odd ones to lane B. Because adjustment entries sometimes carried negative sequence numbers, using != 0 instead of == 1 ensured correct routing even for those edge cases. A senior engineer also knows that this strategy only works well when IDs are roughly uniformly distributed; sequential IDs alternate perfectly, but hash-based IDs may cluster.
What are common interview follow-up questions?
- How would you check if a number is divisible by any N, not just 2?
- What is the difference between % and Math.floorMod for negative numbers?
- Can you determine even/odd without using modulo or bitwise operations?
Visual Overview
┌─────────┐
│ Start │
└────┬────┘
▼
┌──────────┐
│ Read N │
└────┬─────┘
▼
┌────────────────┐
│ N % 2 == 0? │
└──┬──────────┬──┘
│ Yes │ No
▼ ▼
Print Even Print Odd
│ │
└────┬─────┘
▼
┌─────┐
│ End │
└─────┘
Key Takeaway
A number is even when it divides exactly by two and odd otherwise. The test is the modulo operation: if N % 2 equals zero the number is even, and if not it is odd. For input 7, the remainder is 1, so the answer is Odd.
The subtle correctness point is the sign of the remainder in Java. The remainder takes the sign of the dividend, so -7 % 2 is -1, not 1. Testing odd as N % 2 == 1 would misclassify every negative odd number. The robust test is N % 2 != 0, which works for positive, negative, and zero.
The bitwise alternative (N & 1) != 0 is equivalent and always returns 0 or 1, but the modulo form is clearer and the compiler optimizes both identically.
In a banking project at National Bank of India, this classified transaction IDs for load-balanced routing. Even IDs went to lane A and odd ones to lane B. Because some adjustment entries carried negative sequence numbers, comparing the remainder to zero ensured correct routing for all cases.
Remember one sentence: test odd as N % 2 != 0, never N % 2 == 1, because Java's modulo preserves the dividend's sign.
---