Step 01 Beginner Problems — Basic Recursion
Recursion Theory
Recursion is a function that calls itself on a smaller version of the same problem, until the problem is so small the answer is immediate. It is the gateway to trees, graphs, backtracking and dynamic programming — and the topic beginners fear most, usually because nobody showed them the call stack. This lesson builds recursion from its two mandatory parts, shows exactly what the computer does while it runs, and gives you a design method that works on every recursive problem in this sheet.
The two mandatory parts
Every correct recursion has:
1. A base case — the smallest input, answered directly with no further call. factorial(0) = 1; sum of an empty list = 0. 2. A recursive case — the answer expressed using the same function on a strictly smaller input: factorial(n) = n × factorial(n - 1).
Two failure modes mirror the two parts: a missing or unreachable base case means the calls never stop (infinite recursion → stack overflow), and a recursive call that does not shrink the input (calling f(n) from f(n)) never reaches the base case. Before anything else, check both, every time.
The call stack: what the computer actually does
Each call gets its own stack frame — its own copy of the parameters and local variables — pushed onto the call stack. Trace factorial(3):
factorial(3) waits for factorial(2) factorial(2) waits for factorial(1) factorial(1) waits for factorial(0) factorial(0) returns 1 <- base case factorial(1) returns 1 × 1 = 1 factorial(2) returns 2 × 1 = 2 factorial(3) returns 3 × 2 = 6
The frames pile up on the way down (winding) and resolve on the way back up (unwinding). Nothing is magical: the answer is assembled during unwinding. The stack is also the cost — depth d means d live frames, O(d) space, and a hard limit (Python's default recursion limit is about 1000) beyond which the program crashes.
def factorial(n):
if n == 0: # base case
return 1
return n * factorial(n - 1) # recursive case
print(factorial(5)) # 120Reading a recursion as a tree
When a function makes two calls, like naive Fibonacci — fib(n) = fib(n-1) + fib(n-2) — the calls form a tree, and the tree exposes the scandal: fib(3) is computed twice inside fib(5), fib(2) three times, and the tree grows to roughly 2^n nodes. Drawing three levels of the tree is the fastest way to see whether a recursion is linear (one call — a chain, O(n)) or branching (a tree, potentially exponential).
Recognising repeated subproblems in the tree is also the birth of dynamic programming: memoisation stores each subproblem's answer so the tree collapses into a chain. You will meet that properly in the DP module; here, just learn to draw the tree.
The design method: define, trust, combine
Write recursions in three steps. (1) Define the contract in one sentence: 'sum_first(n) returns 1 + 2 + ... + n.' (2) Write the base case from the contract: sum_first(0) = 0. (3) Assume — on faith — that the smaller call already fulfils the contract, and write only the combining step: sum_first(n) = n + sum_first(n - 1).
The faith step is the whole trick: you are not supposed to mentally simulate the entire descent while designing. Define the contract precisely, make the input smaller, combine honestly — then verify with a small trace afterwards. Beginners who try to hold the whole stack in their head while writing drown; professionals define and trust.
Recursion vs iteration, and the common mistakes
Any recursion can be rewritten as a loop (sometimes with an explicit stack), and loops use O(1) extra space where recursion uses O(depth). Prefer iteration for simple linear scans in production code; prefer recursion when the problem is naturally self-similar (trees, divide and conquer, backtracking) because the recursive form is dramatically clearer.
Mistake list: missing base case; base case that the recursion never reaches (shrinking by 2 from an odd start past a base of exactly 0); doing work with a global variable when a return value was the contract; and assuming recursion is 'slower' — for the same complexity class the difference is a constant factor, while a wrong complexity class (like naive Fibonacci) is the real killer.
Key points
- Every recursion = a base case answered directly + a recursive case on a strictly smaller input.
- Each call owns a stack frame; answers assemble while the stack unwinds — depth d costs O(d) space.
- One recursive call is a chain, O(n); two calls form a tree that can explode to O(2^n) — draw three levels to check.
- Design by contract: define what the function returns, write the base case, trust the smaller call, write only the combine step.
- Repeated subproblems in the call tree are the signal for memoisation/DP later.
- Check the two fatal flaws first: base case missing/unreachable, and an input that never shrinks.