Fibonacci Using Recursion in DSA C++ - Time & Space Complexity
We want to understand how the time taken by the recursive Fibonacci function grows as the input number increases.
Specifically, how many steps does it take to find the nth Fibonacci number?
Analyze the time complexity of the following code snippet.
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
This code calculates the nth Fibonacci number by calling itself twice for smaller values until it reaches the base cases.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two recursive calls per function call.
- How many times: Each call splits into two more calls until reaching base cases, causing many repeated calculations.
Each number requires calculating two smaller Fibonacci numbers, which themselves require two smaller calls, and so on.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 177 calls |
| 20 | About 21,891 calls |
| 30 | About 2,692,537 calls |
Pattern observation: The number of calls grows very fast, roughly doubling with each increase in n.
Time Complexity: O(2^n)
This means the time needed roughly doubles every time n increases by one, making it very slow for large n.
[X] Wrong: "The recursive Fibonacci runs in linear time because it just counts down from n to 0."
[OK] Correct: Each call makes two more calls, so the total number of calls grows exponentially, not linearly.
Understanding this recursive pattern helps you recognize when recursion leads to repeated work and why optimization or different approaches are needed.
"What if we add memoization to store results of previous calls? How would the time complexity change?"
Practice
Solution
Step 1: Understand Fibonacci sequence definition
The Fibonacci sequence starts with 0 and 1, and each next number is the sum of the two before it.Step 2: Relate to recursion
Recursion calls the function for the two previous numbers and adds their results to get the current number.Final Answer:
Each number is the sum of the two preceding numbers starting from 0 and 1 -> Option BQuick Check:
Fibonacci = sum of two previous numbers [OK]
- Thinking Fibonacci multiplies previous numbers
- Confusing subtraction or squares with Fibonacci
- Ignoring the base cases 0 and 1
Solution
Step 1: Identify correct Fibonacci base cases
Fibonacci sequence starts with 0 at position 0 and 1 at position 1, so base cases must return these values.Step 2: Match base cases to code
if (n == 0) return 0; if (n == 1) return 1; correctly returns 0 for n=0 and 1 for n=1, matching Fibonacci definition.Final Answer:
if (n == 0) return 0; if (n == 1) return 1; -> Option CQuick Check:
Base cases match Fibonacci start [OK]
- Using wrong base values like 1 for n=0
- Returning n for n <= 2 which is incorrect
- Confusing base cases with general recursive step
int fib(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n-1) + fib(n-2);
}
int main() {
std::cout << fib(5);
return 0;
}Solution
Step 1: Calculate fib(5) using recursion
fib(5) = fib(4) + fib(3). Calculate fib(4) = fib(3) + fib(2), fib(3) = fib(2) + fib(1), and so on until base cases.Step 2: Evaluate values step-by-step
fib(0)=0, fib(1)=1, fib(2)=1, fib(3)=2, fib(4)=3, fib(5)=5.Final Answer:
5 -> Option AQuick Check:
fib(5) = 5 [OK]
- Calculating fib(5) as 8 or 13 by mistake
- Confusing index starting point
- Skipping base cases in calculation
int fib(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n-2) + fib(n-1);
}Solution
Step 1: Check base cases
Base cases for n==0 return 0 and n==1 return 1 are correct for Fibonacci.Step 2: Check recursive step
fib(n-2) + fib(n-1) equals fib(n-1) + fib(n-2) because addition is commutative. Function computes correct values. No error.Final Answer:
No error, function is correct -> Option AQuick Check:
Addition commutative, no error [OK]
- Thinking recursive call order must be strictly n-1 first
- Assuming reversed order changes the result
- Overlooking commutativity of addition
fib called when computing fib(4) using the recursive function below?int fib(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n-1) + fib(n-2);
}Solution
Step 1: Trace calls for fib(4)
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 total calls
Calls: fib(4), fib(3), fib(2), fib(1), fib(2), fib(1), fib(0), fib(1), fib(0) = 9 calls total.Final Answer:
9 -> Option DQuick Check:
Count recursive calls carefully = 9 [OK]
- Counting only unique calls
- Ignoring repeated calls for same n
- Underestimating recursion tree size
Ready to prove it?
Pass the test and this pattern moves unknown
