Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Append `n`, then recurse on `n-1`; stop when `n < 1`. Swapping the two lines would print 1..n ascending instead.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you understand how pre- vs post-order placement controls output order.
Where candidates slip
Wrong visit/recurse order, or no base case.
How to stand out
Connect pre/post-order here to tree traversal order.
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
Pre-order recursion
1Hint 1Explore hintHide hint
Emit n before or after the recursive call — which gives descending?
2Hint 2Explore hintHide hint
Visit-then-recurse = descending; recurse-then-visit = ascending.
3Hint 3Explore hintHide hint
Base case stops at n < 1.
A · Core idea
Print n down to 1 by emitting n before recursing on n−1. The order of the 'visit' relative to the recursive call decides ascending vs descending — the core lesson.
Pre-order vs post-order. Doing the work before the recursive call (pre-order) prints n first, then n−1, …, giving descending order. Doing it after (post-order) prints the deepest value first, giving ascending 1..n. Swapping two lines flips the entire output — a powerful demonstration of how the call stack sequences work.
Why this matters broadly. The same pre/post-order distinction governs tree traversals (visit node before vs after children). Internalising it here pays off on trees later.
Base case n < 1 stops the descent; depth is O(n).
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
n = 3n=3
emit 3, recurse(2)
"3""3 2 1"C · Code with teaching comments
D · Why it works and complexityO(n) timeO(n) stack spaceOpen the invariant and proofExplore the proofHide the proof
Where you place the 'visit' relative to the recursive call flips the output order — a classic recursion insight.
ComplexityO(n) time, O(n) stack space — n calls, each emitting one value, with O(n) stack depth.
E · Edge cases and pitfalls4n = 1 — "1".Explore edge casesHide edge cases
Edge cases handled
n = 1"1".n = 5"5 4 3 2 1".
Pitfalls
- Recursing before appending (prints ascending).
- Missing base case n < 1.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic String countDown(int n) { StringBuilder sb = new StringBuilder(); helper(n, sb); return sb.toString().trim(); }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Recursing before appending (prints ascending).
- Missing base case n < 1.
Revise
Carry the pattern into the interview
Pattern to remember
Pre-order vs post-order
Code pattern to memorize
visit(n); rec(n-1) -> descending (swap -> ascending)Rules to remember
- 1Visit then recurse = descending.
- 2Recurse then visit = ascending.
- 3Base n<1.
Use this pattern when
- Understanding pre/post-order recursion
Interview takeaway
Work-before-recursion vs after flips the output order.
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: Pre-order vs post-order. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups