Lesson · Methods
Recursion: a call stack you can see
12 min readRecursion
A method that calls itself, held together by a base case and the stack. Every recursion has an iterative twin, a stack cost, and a failure mode named StackOverflowError.
Base case, recursive case, progress
A working recursion has exactly three parts. The base case answers directly for the smallest input. The recursive case calls the method on a strictly smaller input. The progress condition guarantees the input shrinks toward the base case. Remove any one and the method either loops forever or returns nonsense.
factorial(0) = 1 is the base; factorial(n) = n * factorial(n - 1) recurses on n minus one. Each waiting call holds its own copy of n in its stack frame, and the multiplication happens on the way back up: the frames unwind, each returning its product to the frame below.
Call stack · factorial(4)
stack empty
Top of stack
empty
Final answer
not yet
factorial(4) is called. The stack is empty; the first frame is about to be pushed. Every recursion starts exactly this way.
The call stack is the real cost
Iteration keeps its state in one frame that loops; recursion keeps each stage in its own frame that waits. That is the entire trade: recursion can express winding and unwinding naturally (trees, divide and conquer) at the price of stack space proportional to depth. Java has no tail-call optimisation: a tail-recursive method still grows the stack, so deep recursions (10 to 100 thousand frames) end in StackOverflowError where the loop version simply runs.
Reading a stack trace thus becomes second nature: the repeated factorial lines are the frames, and the bottom is where the first call began. When recursion misbehaves, the trace is a picture of the bug.
Every recursion has an iterative twin
Linear recursions convert directly: keep the shrinking state in a loop variable and the accumulated result in another. factorial to a running product, Fibonacci to two rolling variables, digit sums to the peel loop from the practice topic. When a linear recursion overflows the stack, this conversion is the fix, not a bigger -Xss.
The conversions stop being mechanical for two shapes: multiple self-calls (tree-style recursion, where an explicit stack mirrors the call stack) and mutually recursive methods (f calls g calls f). Recognising which shape you have decides whether the iterative rewrite is a five-line loop or a project.
When recursion is the right tool
Recursion wins when the problem is defined in terms of itself: directory trees, parse trees, quicksort and mergesort's two halves, backtracking searches (the N-Queens topic later proves the pattern). In these cases the recursive version is the clearest statement of the algorithm, and clarity beats the constant factor.
It loses for anything linear on hot paths, where the loop is both clearer and cheaper. The mature position is neither preference: the structure chooses the form. A tree wants recursion the way a range wants a for loop.
Naive recursion has exponential blowup hiding in it
Fibonacci by double recursion re-computes the same subtrees again and again: fib(40) makes over 300 million calls for a single answer. The fix is memoisation, caching each result once computed, after which the recursion is linear. Whenever overlapping subproblems appear, add a cache before asking for a bigger stack.
Interview angles
What are the three necessary parts of a recursion?+
A base case that answers directly, a recursive case on a strictly smaller input, and progress that guarantees the shrinking reaches the base. Missing base or missing progress are the two failure modes; the third, non-shrinking input, is the same bug wearing a costume.
Does Java optimise tail recursion?+
No. Tail-call optimisation is absent by design, so even a properly shaped tail recursion grows the stack frame by frame. Deep recursions must be converted to loops or given an explicit stack.
When is recursion worse than iteration?+
On linear problems and hot paths: it costs stack space, frame setup and often a constant factor, with no clarity gain. It earns its place when the data or the definition is recursive: trees, divide and conquer, backtracking.