Recursion Explained: Base Case, Call Stack and Memoization
Blog / Recursion Explained: Base Case, Call Stack and Memoization
What actually is recursion? And why do interviewers love asking about it?
The Base Case and the Recursive Case
Recursion is a function that calls itself to solve a smaller version of the same problem.Every recursive function needs two specific parts. The first is a base case that returns an answer straight away without calling itself. The second is a recursive case that calls itself with an input closer to the base case.Here is a function to calculate a factorial.def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1)
How the Call Stack Runs It
Let us walk through calling factorial(4). Each function call gets its own stack frame with its own copy of n.These frames pile up in memory on the stack while each one waits for the call below it to finish.
Missing the Base Case
What happens if the base case is missing or never reached? The function just keeps calling itself.The stack keeps growing until the program runs out of memory and crashes with a stack overflow.The exact same thing happens when the input is too deep, even with a correct base case. Python has a default recursion limit of exactly 1000 frames. If your function exceeds this limit, Python raises a RecursionError to prevent a system crash.Recursion on a Tree
Recursion fits perfectly with data that is nested to a depth you do not know in advance. A simple loop does not fit a nested tree structure well.Finding the maximum depth of a binary tree is a great example.def max_depth(node):
if node is None:
return 0
left = max_depth(node.left)
right = max_depth(node.right)
return 1 + max(left, right)
Repeated Work
Recursion makes tree logic simple, but it can also hide severe performance problems.A naive recursive Fibonacci function calls fib(n - 1) and fib(n - 2). Those calls repeat the exact same subproblems again and again.def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)
from functools import cache
@cache
def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)

Recursion vs Iteration
Any recursive function can be rewritten as a loop. You can just use an explicit stack data structure where needed.Recursion is often shorter and clearer for trees and nested data. A loop avoids the memory overhead of a function call per step and the risk of a stack overflow.Some languages reuse the current stack frame when the recursive call is the absolute final action evaluated in the function. This is called tail recursion.
Recap
| Term | What it means |
|---|---|
| Recursion | A function that calls itself |
| Base case | The condition that stops the recursion |
| Recursive case | The part that calls the function again |
| Stack frame | Memory allocated for one function call |
| Unwinding | Returning values back up the call stack |
| Stack overflow | A crash caused by too many stack frames |
| Memoization | Caching results to avoid repeated work |
| Tail recursion | A memory optimization for final recursive calls |
About TechPrep
Never walk into a technical interview unprepared again. TechPrep empowers software engineers to stop guessing and start getting offers. We provide the exact questions asked by tech companies across Data Structures & Algorithms, System Design, Low-Level Design & AI coding rounds. Don't leave your career up to chance. Join thousands of engineers who have successfully navigated the tech hiring maze and landed roles at top tech companies.