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.
python
def factorial(n): if n == 1: return 1 return n * factorial(n - 1)
The base case checks if n is 1 and returns 1. The recursive case multiplies n by another call to factorial with an input of 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.
The call stack for factorial(4): factorial(1) returns 1 while the three frames below it wait
When factorial(1) hits the base case, it returns 1. Then the stack begins unwinding in reverse order.Each frame multiplies its own n by the returned value and returns the result. This continues until factorial(4) finally returns 24.

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.
python
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)
The base case is an empty node, which has a depth of 0. Otherwise, the depth is 1 plus the larger of the left and right subtrees.The trick is to trust the recursive call. You just assume the call already returns the right answer for the smaller tree. You only write the logic to combine those answers on the way back up.

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.
python
def fib(n): if n <= 1: return n return fib(n - 1) + fib(n - 2)
The number of calls grows exponentially with each step up in n, hitting O(2ⁿ) time.This is where memoization comes in. Memoization stores each result the first time it is computed. Later calls simply return the stored result instead of doing the work again.
python
from functools import cache @cache def fib(n): if n <= 1: return n return fib(n - 1) + fib(n - 2)
The call tree of fib(4) with a cache: calls in green come from the cache, so their own calls never run
This immediately brings the time complexity down to O(n).

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.
Normal recursion keeps four frames waiting to multiply. A tail call has nothing left to do, so one frame is reused
Python and Java do not support this optimization, so it will not save memory there.Watch out for a common interview trap regarding memory. Recursion uses O(n) extra space for the call stack when the recursion goes n calls deep, so a recursive solution is not O(1) space.

Recap

TermWhat it means
RecursionA function that calls itself
Base caseThe condition that stops the recursion
Recursive caseThe part that calls the function again
Stack frameMemory allocated for one function call
UnwindingReturning values back up the call stack
Stack overflowA crash caused by too many stack frames
MemoizationCaching results to avoid repeated work
Tail recursionA 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.