Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Either filter with `i % 2 == 0`, or more efficiently start at 2 and step by 2.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you prefer stepping over filtering for a regular pattern.
Where candidates slip
Filtering every value, or an off-by-one on the bound.
How to stand out
Note stepping halves the iterations versus filtering.
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
Step by two
1Hint 1Explore hintHide hint
You can filter with `% 2 == 0`, or avoid the test entirely — how?
2Hint 2Explore hintHide hint
Start at 2 and step by 2 to hit only evens.
3Hint 3Explore hintHide hint
What should happen when n < 2?
A · Core idea
Produce the even numbers up to n. Two equivalent approaches: filter every value with i % 2 == 0, or step the counter by 2 starting at 2.
Why stepping is cleaner. Starting at 2 and doing i += 2 visits only the evens — no per-value modulo test and half the iterations. It's the natural fit when the pattern is regular and known in advance.
Why start at 2, not 0. 0 is technically even, but these problems usually want the positive evens 2, 4, …, so start at 2. Confirm the convention.
Edge: n < 2. There are no evens to print, so the result is the empty string — the loop simply never runs.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 5i=2
append 2
"2""2 4"C · Code with teaching comments
D · Why it works and complexityO(n) timeO(n) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n) time, O(n) space — about n/2 iterations, each appending one value.
E · Edge cases and pitfalls4n = 10 — "2 4 6 8 10".Explore edge casesHide edge cases
Edge cases handled
n = 10"2 4 6 8 10".n = 1No evens → "".
Pitfalls
- Including odd numbers.
- Starting the step at 0 (0 is even but usually excluded).
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String printEven(int n) { StringBuilder sb = new StringBuilder(); for (int i = 2; i <= n; i += 2) { if (sb.length() > 0) sb.append(' '); sb.append(i);Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Including odd numbers.
- Starting the step at 0 (0 is even but usually excluded here).
Revise
Carry the pattern into the interview
Pattern to remember
Stepped loop
Code pattern to memorize
for i in range(2, n+1, 2): append(i)Rules to remember
- 1Start at 2, step by 2.
- 2Or filter i % 2 == 0.
- 3Handle n < 2 → empty.
Use this pattern when
- Regular stepped sequences
Interview takeaway
Stepping the counter beats filtering when the pattern is regular.
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: Stepped loop. Master it on small inputs and it transfers to harder problems.