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.
Specifically, how many times the function calls itself and what that means for performance.
Analyze the time complexity of the following code snippet.
int factorial(int n) {
if (n == 0) {
return 1;
}
return n * factorial(n - 1);
}
This code calculates the factorial of a number using recursion by calling itself with a smaller number 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 + 1 times until it reaches the base case (n == 0).
Each increase in n adds one more function call, so the total calls grow directly with n.
| Input Size (n) | Approx. Operations (Function Calls) |
|---|---|
| 10 | 11 calls |
| 100 | 101 calls |
| 1000 | 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; doubling n roughly doubles the work.
[X] Wrong: "Recursion always makes the program slower exponentially because it calls itself many times."
[OK] Correct: In this case, each call happens once per input size, so the growth is linear, not exponential.
Understanding how recursion grows helps you explain your code clearly and reason about performance in interviews.
"What if the recursive function called itself twice each time? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of base case in recursion
A base case stops the recursion by providing a condition to end the repeated calls.Step 2: Analyze what happens without a base case
Without a base case, the function keeps calling itself endlessly, filling the call stack until it overflows.Final Answer:
It causes infinite recursion and eventually a stack overflow error. -> Option BQuick Check:
No base case = infinite recursion [OK]
- Thinking recursion stops automatically without base case
- Confusing recursion with loops
- Assuming function returns without base case
Solution
Step 1: Check base case correctness
Factorial base case is when n == 0, return 1.Step 2: Check recursive call and operation
Recursive call should be factorial(n - 1) multiplied by n.Final Answer:
int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } -> Option DQuick Check:
Base case n==0 and multiply with factorial(n-1) [OK]
- Using while loop instead of recursion
- Wrong base case value or return
- Incorrect recursive call argument
void printNumbers(int n) {
if (n == 0) return;
printNumbers(n - 1);
printf("%d ", n);
}
int main() {
printNumbers(3);
return 0;
}Solution
Step 1: Trace recursive calls
printNumbers(3) calls printNumbers(2), which calls printNumbers(1), then printNumbers(0) returns immediately.Step 2: Print values after recursive calls return
After reaching base case, prints 1, then 2, then 3 in order.Final Answer:
1 2 3 -> Option AQuick Check:
Print after recursion = ascending order [OK]
- Printing before recursive call (would reverse order)
- Including 0 in output incorrectly
- Confusing base case behavior
int sum(int n) {
if (n == 0) return 0;
return n + sum(n);
}Solution
Step 1: Analyze recursive call argument
The function calls sum(n) again without reducing n, causing infinite recursion.Step 2: Correct recursive call to reduce problem size
It should call sum(n - 1) to move towards base case and stop recursion.Final Answer:
The recursive call should be sum(n - 1) instead of sum(n). -> Option AQuick Check:
Recursive call argument must decrease [OK]
- Calling sum(n) again causing infinite recursion
- Changing base case incorrectly
- Using multiplication instead of addition
void reverse(char *str, int start, int end) {
if (start >= end) return;
char temp = str[start];
str[start] = str[end];
str[end] = temp;
reverse(str, start + 1, end - 1);
}
int main() {
char s[] = "hello";
reverse(s, 0, 4);
printf("%s", s);
return 0;
}What will be printed after running this program?
Solution
Step 1: Understand the recursive swapping
The function swaps characters at start and end indexes, then calls itself with start+1 and end-1.Step 2: Trace swaps for "hello"
Swaps 'h' and 'o' -> "oellh", then swaps 'e' and 'l' -> "olleh", then stops when start >= end.Final Answer:
olleh -> Option CQuick Check:
Recursive swaps reverse string [OK]
- Not updating start and end indexes correctly
- Confusing base case condition
- Assuming string prints unchanged
Ready to prove it?
Pass the test and this pattern moves unknown
