The Even Checkpoint
An easy Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.
- Domain
- Python
- Difficulty
- easy
- Seniority
- L3
- Asked in
- i360
Problem
A hot parity check runs on every record in a tight loop, and profiling flagged the modulo it uses as the bottleneck. Given an integer n (which may be negative), return True if n is even and False otherwise, without using the modulo operator.
Is this number in the even club? Prove it the fast way.
Example 1
Input:
n = 4
Output:
true
Example 2
Input:
n = 7
Output:
false
Worked solution and explanation
What this problem really is
Under its 'check for even' costume this is really a question about where parity lives in a number: the ones-bit, and nothing else. Ban the modulo operator and the candidate has to reach for that bit directly. Anyone can write n & 1; what separates people is returning a real True/False instead of the raw 1 or 0, and knowing that in Python the low-bit test needs no special-casing for negatives. Return the bit itself and every downstream if is_even_bitwise(x): starts lying to you.
Break it down
Step 1: Isolate the low bit
n & 1 keeps only the ones-place bit and discards everything above it. That single bit carries the entire even/odd answer.
Step 2: Turn the bit into a real boolean
A raw bit is 0 or 1, an int, not a bool. Compare it and bind the result to a named variable: even = (n & 1) == 0 holds a genuine True/False, which is what callers branch on, and returning it keeps the intent readable.
The solution
def is_even_bitwise(n: int) -> bool:
even = (n & 1) == 0
return evenCommon follow-up questions
- How does this differ from using modulo, and why might the bit test be faster? (Tests performance awareness: bitwise AND is a single CPU instruction versus division for modulo.)
- How would you check if a number is a power of 2? (Tests the `n & (n - 1) == 0` pattern.)
- What other bitwise tricks show up in data engineering? (Tests knowledge of bit masking, XOR for deduplication, and hash function internals.)