Recursion Concept and Call Stack Visualization in DSA Java - Time & Space Complexity
When using recursion, it is important to know how many times the function calls itself.
We want to understand how the number of calls grows as the input gets bigger.
Analyze the time complexity of the following code snippet.
public 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 smaller values.
- Primary operation: Recursive call to factorial with n-1
- How many times: The function calls itself once for each number from n down to 1, so n times
Each increase in input n adds one more recursive call, so the total calls grow directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 calls |
| 100 | About 100 calls |
| 1000 | About 1000 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 size of the input.
[X] Wrong: "Recursion always means exponential time because it calls itself multiple times."
[OK] Correct: In this example, the function calls itself only once per step, so the time grows linearly, not exponentially.
Understanding how recursion unfolds helps you explain your code clearly and shows you can reason about performance.
"What if the function called itself twice each time? How would the time complexity change?"
Practice
Solution
Step 1: Understand recursion without base case
A recursive function without a stopping condition keeps calling itself endlessly.Step 2: Effect on call stack
Each call adds a new frame to the call stack until memory runs out, causing a stack overflow error.Final Answer:
It causes infinite recursion leading to a stack overflow error. -> Option CQuick Check:
Missing base case = stack overflow [OK]
- Assuming recursion stops automatically
- Thinking recursion converts to loop
- Believing it returns null without base case
Solution
Step 1: Check method call syntax
public void recurse() { recurse(); } correctly calls the method recurse() inside itself using parentheses.Step 2: Identify syntax errors in other options
public void recurse() { return recurse(); } tries to return void method call, invalid. public void recurse() { if(true recurse(); } missing parenthesis after if condition, invalid syntax. public void recurse() { recurse; } misses parentheses, invalid call.Final Answer:
public void recurse() { recurse(); } -> Option AQuick Check:
Method call needs parentheses [OK]
- Omitting parentheses in recursive call
- Returning void method call
- Using invalid syntax inside method
public class Test {
public static void printNumbers(int n) {
if (n == 0) return;
System.out.print(n + " ");
printNumbers(n - 1);
}
public static void main(String[] args) {
printNumbers(3);
}
}Solution
Step 1: Trace recursive calls and prints
printNumbers(3) prints 3, then calls printNumbers(2), prints 2, calls printNumbers(1), prints 1, then calls printNumbers(0) which returns.Step 2: Output order
Numbers print before recursive call, so output is "3 2 1 " with no zero printed.Final Answer:
3 2 1 -> Option DQuick Check:
Print before recursion = descending order [OK]
- Expecting ascending order output
- Including zero in output
- Confusing print order with recursion
public static int factorial(int n) {
if (n == 1) return 1;
else return n * factorial(n);
}Solution
Step 1: Analyze recursive call parameter
The method calls factorial(n) again without reducing n, causing infinite recursion.Step 2: Correct recursive call
It should call factorial(n - 1) to reduce n and reach base case.Final Answer:
Change recursive call to factorial(n - 1) -> Option BQuick Check:
Recursive call must reduce parameter [OK]
- Calling factorial with same argument causing infinite recursion
- Wrong base case value
- Changing return type unnecessarily
public static int sumDigits(int n) {
if (n == 0) return 0;
return (n % 10) + sumDigits(n / 10);
}What is the output of
sumDigits(1234) and explain how the call stack evolves?Solution
Step 1: Trace recursive calls and sums
sumDigits(1234) = 4 + sumDigits(123), sumDigits(123) = 3 + sumDigits(12), sumDigits(12) = 2 + sumDigits(1), sumDigits(1) = 1 + sumDigits(0), sumDigits(0) = 0.Step 2: Calculate total sum and call stack order
Total sum = 4+3+2+1+0 = 10. The call stack grows with calls: 1234 -> 123 -> 12 -> 1 -> 0, then unwinds adding digits.Final Answer:
10; Calls: sumDigits(1234) -> sumDigits(123) -> sumDigits(12) -> sumDigits(1) -> sumDigits(0) -> Option AQuick Check:
Sum digits = 10, call stack grows with decreasing n [OK]
- Adding digits incorrectly
- Misunderstanding call stack order
- Confusing sum with original number
Ready to prove it?
Pass the test and this pattern moves unknown
