Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Reading their recursion

โญ beginnerโณ 10 min read๐Ÿ“ Lesson 55 of 256

Print-before/print-after, counting calls, overflow signatures.

The "what does this print" genre, decoded:

public static void puzzle(int n) {
    if (n <= 0) {
        return;
    }
    System.out.print(n + " ");
    puzzle(n - 2);
    System.out.print(n + " ");
}

puzzle(5) prints 5 3 1 3 5 โ€” before the call for the descent, after it for the unwind. Number of prints: 2 per call that survives the base check (3 calls โ†’ 6 prints).

Mental rules:

  • Code before the recursive call runs top-down (descent order).
  • Code after the recursive call runs bottom-up (unwind order).
  • Total calls = every invocation including base-case hits.
  • Missing base case or a branch that can't reach it โ†’ StackOverflowError, never "runs forever" visibly.

Also know: recursion can always be replaced by a loop, but the exam wants you to read their recursion, not rewrite it.

โšก Now practice

Ready to Code
Recursion repsLadders, palindromes, digit/index recursion, trace debugging.
5 challenges ยท ยท ~55 min