Fibonacci Using Recursion in DSA Python - 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.
def fibonacci(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 each number greater than 1.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two recursive calls for each call when n > 1.
- How many times: The function calls itself repeatedly, creating a tree of calls that grows quickly as n increases.
Each call makes two more calls until reaching the base case, so the number of calls grows very fast.
| 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 roughly doubles with each increase in n, leading to very fast growth.
Time Complexity: O(2^n)
This means the time needed doubles with each increase in n, making it very slow for large numbers.
[X] Wrong: "The recursive Fibonacci runs in linear time because it just counts down from n to 1."
[OK] Correct: Each call makes two more calls, so the total calls grow exponentially, not linearly.
Understanding this recursive pattern helps you see why some solutions are slow and how to improve them, a key skill in interviews.
"What if we used memoization to store results of previous calls? How would the time complexity change?"
Practice
Solution
Step 1: Understand Fibonacci sequence definition
The Fibonacci sequence is defined such that each number is the sum of the two numbers before it.Step 2: Relate to recursion
Recursion calculates this by calling the function for the two previous numbers and adding their results.Final Answer:
Each number is the sum of the two preceding numbers -> Option DQuick Check:
Fibonacci = sum of two previous numbers [OK]
- Thinking Fibonacci multiplies previous numbers
- Confusing difference with sum
- Assuming Fibonacci squares previous number
Solution
Step 1: Identify base cases for Fibonacci
Fibonacci sequence starts with 0 at position 0 and 1 at position 1, so base cases are n == 0 and n == 1.Step 2: Check syntax correctness
The correct syntax returns n when n is 0 or 1, matching the base cases.Final Answer:
if n == 0 or n == 1: return n -> Option AQuick Check:
Base cases return n for 0 or 1 [OK]
- Using wrong base case values
- Returning 0 for n == 1
- Missing base case for n == 0
def fib(n):
if n == 0 or n == 1:
return n
else:
return fib(n-1) + fib(n-2)
print(fib(4))Solution
Step 1: Trace fib(4) calls
fib(4) = fib(3) + fib(2); fib(3) = fib(2) + fib(1); fib(2) = fib(1) + fib(0)Step 2: Calculate values bottom-up
fib(0)=0, fib(1)=1; fib(2)=1+0=1; fib(3)=1+1=2; fib(4)=2+1=3Final Answer:
3 -> Option CQuick Check:
fib(4) = 3 [OK]
- Confusing fib(4) with fib(5)
- Adding wrong values for fib(2) or fib(3)
- Forgetting base cases
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) - fib(n-2)Solution
Step 1: Check base cases
Base cases for n == 0 and n == 1 are correct and return proper values.Step 2: Analyze recursive step
The recursive step uses subtraction (fib(n-1) - fib(n-2)) instead of addition, which is incorrect for Fibonacci.Final Answer:
The recursive step uses subtraction instead of addition -> Option BQuick Check:
Recursive step must add previous two values [OK]
- Using subtraction in recursive step
- Misunderstanding base cases
- Missing return keyword
fib(5) using the naive recursive Fibonacci function?def fib(n):
if n == 0 or n == 1:
return n
else:
return fib(n-1) + fib(n-2)Solution
Step 1: Count calls for fib(5)
fib(5) calls fib(4) and fib(3); fib(4) calls fib(3) and fib(2); fib(3) calls fib(2) and fib(1), etc.Step 2: Calculate total calls
Total calls = 15 (including repeated calls for fib(3), fib(2), fib(1), fib(0)) as the recursion tree expands.Final Answer:
15 -> Option AQuick Check:
Naive recursion calls grow exponentially; fib(5) = 15 calls [OK]
- Counting only unique calls
- Ignoring repeated calls in recursion
- Underestimating exponential growth
Ready to prove it?
Pass the test and this pattern moves unknown
