Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Keep two counters. For each element, increment `even` when `x % 2 == 0`, otherwise `odd`.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you apply a robust parity test inside a counting loop.
Where candidates slip
The `% 2 == 1` negative bug, or swapped output order.
How to stand out
Offer the single-counter `odd = n − even` simplification.
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
Two counters
1Hint 1Explore hintHide hint
One pass, classify each element, bump a counter.
2Hint 2Explore hintHide hint
Use `% 2 == 0` for even so negatives are handled.
3Hint 3Explore hintHide hint
You could track only evens and derive odds as n − even.
A · Core idea
Classify each element by parity and tally it: this is the classify-and-count pattern with one counter per category.
Why test `% 2 == 0`. As in even-or-odd, % can be negative in Java/C++ (-3 % 2 == -1), so detecting odd via == 1 misclassifies negative odds. Test evenness with == 0 and treat the else branch as odd.
Why two counters (or one). Two counters read clearly. You can keep just an even counter and compute odd = n − even at the end, since every element is exactly one or the other — a nice space micro-optimisation worth mentioning.
Order of the result. Return [even, odd] in the agreed order; swapping them is a silent correctness bug the tests will catch.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, 2, 3, 4, 5]1
odd
even=0, odd=1[2, 3]C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n) time, O(1) space — one parity test and one increment per element.
E · Edge cases and pitfalls5nums = [2, 4, 6] — All even → [3, 0].Explore edge casesHide edge cases
Edge cases handled
nums = [2, 4, 6]All even → [3, 0].nums = []Nothing to count → [0, 0].nums = [-3, -2]Parity via `% 2 == 0` correctly gives [1, 1].
Pitfalls
- Detecting odd with `% 2 == 1` (wrong for negatives).
- Returning the counts in the wrong order.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int[] countEvenOdd(int[] nums) { int even = 0, odd = 0; for (int x : nums) { if (x % 2 == 0) even++; else odd++;Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Testing `x % 2 == 1` for odd — fails on negative odds.
- Returning the counts in the wrong order.
Revise
Carry the pattern into the interview
Pattern to remember
Classify-and-count
Code pattern to memorize
for x: if x%2==0 even++ else odd++Rules to remember
- 1Parity via
% 2 == 0. - 2Bump the matching counter.
- 3odd = n − even if you want one counter.
Use this pattern when
- Bucketing elements into a few categories
Interview takeaway
Counting categories is one pass with one counter per category.
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: Classify-and-count. Master it on small inputs and it transfers to harder problems.