Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Check `n % 2`. If it's 0 the number is even, otherwise odd. For negatives prefer `n % 2 == 0` (true) rather than `== 1`, since `-3 % 2` is `-1` in Java/Python.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you know that `%` can be negative and choose a parity test that survives negatives.
Where candidates slip
Detecting odd with `n % 2 == 1`, which fails on negative inputs.
How to stand out
Offer `(n & 1)` as the language-agnostic, faster check and explain the signed-modulo subtlety.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Modulo by 2
1Hint 1Explore hintHide hint
Even means 'divisible by 2'. What operator tells you the remainder?
2Hint 2Explore hintHide hint
Test `n % 2 == 0` for even — and think about what `-3 % 2` returns in Java.
3Hint 3Explore hintHide hint
The bitwise check `(n & 1) == 0` avoids the negative-remainder trap entirely.
A · Core idea
A number is even when it leaves no remainder on division by 2. The modulo operator % answers this directly, so n % 2 == 0 is the test for evenness.
Why you must test `== 0`, never `== 1`. In Java, C, and C++ the % operator follows the sign of the dividend, so -3 % 2 is -1, not 1. Code that checks n % 2 == 1 to detect odd numbers therefore returns the wrong answer for every negative odd value. Always phrase parity as 'the remainder is zero' for even, and treat everything else as odd.
The bitwise alternative. The lowest bit of an integer is 1 exactly when the number is odd, so (n & 1) == 0 tests evenness too. It sidesteps the negative-remainder pitfall entirely (the low bit of -3 is 1) and is a touch faster. Mentioning it is a nice signal.
Python note. Python's % follows the sign of the divisor, so -3 % 2 is 1 there — but the safe habit of testing == 0 still works everywhere, which is exactly why you should default to it.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = -3n % 2 in Java
-3 % 2 = -1 (sign follows dividend)
not 0 → Odd ✓"Odd"C · Code with teaching comments
D · Why it works and complexityO(1) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
The bitwise alternative (n & 1) == 0 avoids the negative-remainder pitfall entirely and is a nice thing to mention.
ComplexityO(1) time, O(1) space — one modulo (or one bitwise AND) — constant work.
E · Edge cases and pitfalls5n = 0 — 0 % 2 == 0 → Even, the correct parity of zero.Explore edge casesHide edge cases
Edge cases handled
n = 00 % 2 == 0 → Even, the correct parity of zero.n = -4Even; `% 2` is 0 for negatives too when you test against 0.n = -7Odd; the bitwise check or the `!= 0` test handles it where `== 1` fails.
Pitfalls
- Testing `n % 2 == 1` for odd — wrong for negative odd numbers in Java/C++.
- Confusing `=` (assignment) with `==` (comparison) inside the condition.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String evenOrOdd(int n) { return (n & 1) == 0 ? "Even" : "Odd"; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Detecting odd with `== 1`
In Java/C++ `%` follows the dividend's sign, so `-3 % 2` is `-1`; `-1 == 1` is false and the number is misclassified.
Explore the code comparisonHide the code comparison
Detecting odd with `== 1`
In Java/C++ `%` follows the dividend's sign, so `-3 % 2` is `-1`; `-1 == 1` is false and the number is misclassified.
Don't write this
boolean odd = (n % 2 == 1); // false for -3, -5, ...Write this instead
boolean odd = (n % 2 != 0); // or (n & 1) == 1Revise
Carry the pattern into the interview
Pattern to remember
Parity via modulo / AND
Code pattern to memorize
even <=> (n % 2 == 0) <=> ((n & 1) == 0)Rules to remember
- 1Even ⟺
n % 2 == 0. - 2Never test
== 1for odd. - 3
n & 1is the safe, fast parity check.
Use this pattern when
- Classifying integers by parity
- Toggling/alternating behaviour every other step
Choose another pattern when
- You wrote `% 2 == 1` to mean odd — fix it for negatives
Interview takeaway
Always phrase parity as 'remainder is zero', never 'remainder is one'.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Parity via modulo / AND. Master it on small inputs and it transfers to harder problems.