Recursion Concept and Call Stack Visualization in DSA C++ - Time & Space Complexity
We want to understand how the time taken by a recursive function grows as the input size increases.
How many times does the function call itself and how does that affect total work?
Analyze the time complexity of the following recursive function.
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
This function calculates the factorial of a number by calling itself with smaller values until it reaches zero.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The function calls itself once per call, reducing n by 1 each time.
- How many times: It repeats exactly n times until it reaches the base case at 0.
Each increase in input n adds one more function call, so the total work grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 11 calls |
| 100 | About 101 calls |
| 1000 | About 1001 calls |
Pattern observation: The number of calls grows linearly as n increases.
Time Complexity: O(n)
This means the time taken grows in a straight line with the input size.
[X] Wrong: "Recursion always means exponential time because it calls itself multiple times."
[OK] Correct: This function calls itself only once per step, so the total calls grow linearly, not exponentially.
Understanding how recursion unfolds and how the call stack grows helps you explain your solutions clearly and shows you grasp the cost behind recursive calls.
"What if the function called itself twice each time (like in Fibonacci)? How would the time complexity change?"
Practice
Solution
Step 1: Understand recursion base case role
The base case stops the recursion by providing a condition to end calls.Step 2: Analyze missing base case effect
Without a base case, the function calls itself endlessly, filling the call stack.Final Answer:
The function keeps calling itself indefinitely, causing a stack overflow -> Option AQuick Check:
No base case = infinite calls [OK]
- Thinking recursion stops automatically
- Assuming default return value ends recursion
- Confusing base case with recursive step
Solution
Step 1: Check base case correctness
int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } uses base case n == 0 returning 1, which is correct for factorial.Step 2: Verify recursive call and operation
int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } multiplies n by factorial(n-1), matching factorial definition.Final Answer:
int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } -> Option BQuick Check:
Correct base case and recursive step = int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } [OK]
- Using while loop instead of recursion
- Wrong base case value
- Incorrect recursive call argument
void printNumbers(int n) {
if (n == 0) return;
printNumbers(n - 1);
std::cout << n << " ";
}
int main() {
printNumbers(3);
return 0;
}Solution
Step 1: Trace recursive calls
printNumbers(3) calls printNumbers(2), then printNumbers(1), then printNumbers(0) which returns immediately.Step 2: Print values after returning from recursion
After reaching base case, prints 1, then 2, then 3 in order.Final Answer:
1 2 3 -> Option CQuick Check:
Print after recursion = ascending order [OK]
- Printing before recursive call (would reverse order)
- Including zero in output
- Confusing call stack order
int sum(int n) {
if (n == 1) return 1;
return n + sum(n);
}Solution
Step 1: Check recursive call argument
The function calls sum(n) again without reducing n, causing infinite recursion.Step 2: Identify fix for recursion
The recursive call should be sum(n - 1) to reduce n and reach base case.Final Answer:
The recursive call uses the same argument n, causing infinite recursion -> Option DQuick Check:
Recursive call argument must decrease [OK]
- Not decreasing argument in recursion
- Wrong base case value
- Confusing addition with multiplication
int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}What is the number of times fib(1) is called when fib(4) is executed?
Solution
Step 1: Expand fib(4) calls
fib(4) calls fib(3) and fib(2). fib(3) calls fib(2) and fib(1). fib(2) calls fib(1) and fib(0).Step 2: Count fib(1) calls
fib(1) is called once from fib(3), once from fib(2) inside fib(3), and once from fib(2) directly called by fib(4). Total 3 times.Final Answer:
3 -> Option AQuick Check:
Count base calls in recursion tree = 3 [OK]
- Counting calls only once per function
- Ignoring repeated calls in recursion
- Confusing fib(1) with fib(0)
Ready to prove it?
Pass the test and this pattern moves unknown
