Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Fold with a guard: add `x` to the total when `x % 2 == 0`. Empty or all-odd inputs give 0.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you compose a fold with a predicate and keep parity robust.
Where candidates slip
The negative-modulo parity bug, or dropping the filter.
How to stand out
Frame it as the general 'sum where predicate holds' template.
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
Filtered accumulate
1Hint 1Explore hintHide hint
Start from array-sum — what one line do you add to skip odds?
2Hint 2Explore hintHide hint
Guard the accumulation with `x % 2 == 0`.
3Hint 3Explore hintHide hint
Seed at 0 so all-odd/empty inputs return 0.
A · Core idea
This is the accumulate fold with a filter: add an element to the running total only when it passes a test (here, x % 2 == 0). Seeding at 0 makes empty and all-odd inputs return 0 for free.
Why the filter goes inside the loop. You walk every element once, and the if decides whether it contributes. This 'fold + guard' shape generalises to 'sum of elements greater than k', 'sum of positives', etc. — only the predicate changes.
Parity correctness. Test % 2 == 0, not % 2 == 1 for the complement, so negative evens (-2, -4) are summed correctly and negative odds are excluded.
Overflow. As with any array sum, accumulate into a long since the filtered total can still be large.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, 2, 3, 4]1
odd, skip
total=06C · 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 a conditional add per element.
E · Edge cases and pitfalls5nums = [1, 3, 5] — No evens → 0.Explore edge casesHide edge cases
Edge cases handled
nums = [1, 3, 5]No evens → 0.nums = [2, 4, 6]All even → 12.nums = [-2, -4]Negative evens summed → -6.
Pitfalls
- Testing oddness with `% 2 == 1` and mishandling negatives.
- Forgetting the filter and summing everything.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic long sumEven(int[] nums) { long total = 0; for (int x : nums) { if (x % 2 == 0) total += x; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Testing oddness with `% 2 == 1` and mishandling negatives.
- Adding all elements and forgetting the filter.
Revise
Carry the pattern into the interview
Pattern to remember
Filtered fold
Code pattern to memorize
total=0; for x: if x%2==0: total+=xRules to remember
- 1Accumulate from 0.
- 2Guard with
% 2 == 0. - 3Empty → 0.
Use this pattern when
- Sum/aggregate of elements matching a predicate
Interview takeaway
A fold plus an `if` filter handles 'sum of those that…' problems.
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: Filtered fold. Master it on small inputs and it transfers to harder problems.