Recursion Concept and Call Stack Visualization in DSA Go - 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 recursive function.
func factorial(n int) int {
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.
Look at what repeats in this recursive function.
- Primary operation: The function calls itself once per call with n-1.
- How many times: It repeats until n reaches 0, so roughly n times.
Each call waits for the next call to finish, stacking up calls.
| 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 with n.
Time Complexity: O(n)
This means the time taken grows directly in proportion to the input size.
[X] Wrong: "Recursion always means exponential time because it calls itself multiple times."
[OK] Correct: This example calls itself only once per call, so it grows linearly, not exponentially.
Understanding how recursive calls stack up helps you explain your code clearly and reason about performance in interviews.
"What if the function called itself twice per call instead of once? How would the time complexity change?"
Practice
Solution
Step 1: Understand recursion without base case
Without a stopping condition, the function keeps calling itself endlessly.Step 2: Effect on call stack
Each call adds a new frame to the call stack, which grows until memory runs out causing stack overflow.Final Answer:
It causes an infinite loop and eventually a stack overflow error -> Option DQuick Check:
Missing base case = infinite recursion [OK]
- Thinking recursion stops automatically
- Confusing recursion with loops
- Ignoring stack overflow possibility
Solution
Step 1: Check function signature and return type
Factorial returns an int, so function must return int type.Step 2: Verify base case and recursive call
Base case is n == 0 returning 1; recursive call multiplies n by factorial(n-1).Final Answer:
func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) } -> Option AQuick Check:
Correct base case and return type = func factorial(n int) int { if n == 0 { return 1 } return n * factorial(n-1) } [OK]
- Missing return type in function signature
- Returning 0 instead of 1 in base case
- Not returning value from function
func printCount(n int) {
if n == 0 {
return
}
fmt.Print(n, " ")
printCount(n-1)
}
func main() {
printCount(3)
}Solution
Step 1: Trace recursive calls and prints
printCount(3) prints 3, then calls printCount(2), which prints 2, then calls printCount(1), which prints 1, then calls printCount(0) which returns.Step 2: Confirm output sequence
Output is printed before recursive call, so order is 3 2 1 with spaces.Final Answer:
3 2 1 -> Option AQuick Check:
Print before recursion = descending order [OK]
- Assuming print happens after recursion
- Including 0 in output
- Confusing order of prints
func sum(n int) int {
if n == 1 {
return 1
}
return n + sum(n)
}Solution
Step 1: Analyze recursive call parameter
The function calls sum(n) again without decreasing n, causing infinite recursion.Step 2: Correct recursive call
It should call sum(n-1) to reduce n and reach base case.Final Answer:
Recursive call uses sum(n) instead of sum(n-1), causing infinite recursion -> Option CQuick Check:
Recursive call must reduce n [OK]
- Calling sum(n) again without decrement
- Wrong base case value
- Not returning result from recursion
mystery(4)? Explain the call stack behavior.func mystery(n int) int {
if n <= 1 {
return n
}
return mystery(n-1) + mystery(n-2)
}
func main() {
fmt.Println(mystery(4))
}Solution
Step 1: Recognize the function as Fibonacci calculation
The function returns n if n <= 1, else sum of mystery(n-1) and mystery(n-2), matching Fibonacci logic.Step 2: Calculate mystery(4) step-by-step
mystery(4) = mystery(3) + mystery(2) mystery(3) = mystery(2) + mystery(1) = (mystery(1)+mystery(0)) + 1 = (1+0)+1=2 mystery(2) = mystery(1) + mystery(0) = 1 + 0 = 1 Sum: 2 + 1 = 3 Wait, re-check carefully: mystery(0)=0, mystery(1)=1 mystery(2)=1+0=1 mystery(3)=1+1=2 mystery(4)=2+1=3 But options show 3 and 5, so re-check Fibonacci sequence: 0,1,1,2,3,5... Actually mystery(4) = 3Final Answer:
3 -> Option BQuick Check:
Fibonacci(4) = 3 [OK]
- Miscounting Fibonacci numbers
- Confusing base cases
- Ignoring call stack repeated calls
Ready to prove it?
Pass the test and this pattern moves unknown
