Fibonacci Using Recursion in DSA Javascript - Time & Space Complexity
We want to understand how the time needed to find a Fibonacci number grows as the input number gets bigger.
How does the number of steps change when we use recursion to find Fibonacci numbers?
Analyze the time complexity of the following code snippet.
function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
This code finds the nth Fibonacci number by calling itself twice for smaller numbers until it reaches the base cases.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two recursive calls for each number greater than 1.
- How many times: The function calls itself many times, growing quickly as n increases.
Each call makes two more calls until reaching the base cases, so the total calls grow 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, growing exponentially.
Time Complexity: O(2^n)
This means the time needed grows very fast, doubling with each step up in n, making it slow for large numbers.
[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 much faster than n, not just counting down once.
Understanding this recursive pattern helps you see why some solutions are slow and how to improve them, a key skill in problem solving.
"What if we add a memory to store results (memoization)? How would the time complexity change?"
Practice
fib(4)?Solution
Step 1: Understand recursive calls for fib(4)
The function calls fib(3) and fib(2) to calculate fib(4).Step 2: Combine results of smaller calls
fib(3) and fib(2) are calculated recursively and their sum is returned as fib(4).Final Answer:
Calculates the 4th Fibonacci number by calling itself with smaller numbers -> Option DQuick Check:
Recursive calls break problem into smaller parts [OK]
- Thinking recursion returns input directly
- Assuming recursion causes infinite loop always
- Confusing recursion with iteration
Solution
Step 1: Identify correct JavaScript syntax for base case
The base case must check if n is 0 or 1 and return n using proper JavaScript syntax.Step 2: Validate each option
if (n <= 1) return n; uses correct syntax: if (n <= 1) return n; Options A and B have syntax errors or wrong operators, D is logically incorrect.Final Answer:
if (n <= 1) return n; -> Option BQuick Check:
Base case uses if (n <= 1) return n; [OK]
- Using assignment (=) instead of comparison (== or ===)
- Missing parentheses in if condition
- Incorrect logical condition for base case
function fib(n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
console.log(fib(5));Solution
Step 1: Trace fib(5) recursive calls
fib(5) = fib(4) + fib(3), fib(4) = fib(3) + fib(2), fib(3) = fib(2) + fib(1), and so on until base cases.Step 2: Calculate values bottom-up
fib(0)=0, fib(1)=1, fib(2)=1, fib(3)=2, fib(4)=3, fib(5)=5.Final Answer:
5 -> Option AQuick Check:
Fibonacci(5) = 5 [OK]
- Confusing index with Fibonacci value
- Miscounting recursive calls
- Assuming fib(5) is 8 or 13 incorrectly
function fib(n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n - 1) - fib(n - 2);
}Solution
Step 1: Check base cases correctness
Base cases for n==0 and n==1 correctly return 0 and 1 respectively.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 AQuick Check:
Fibonacci sums previous two, not subtracts [OK]
- Using wrong operator (+ vs -)
- Confusing base cases
- Assuming syntax error when logic is wrong
Solution
Step 1: Understand repeated calculations in recursion
Recursive Fibonacci recalculates same values many times, causing inefficiency.Step 2: Apply memoization to store results
Memoization saves results of fib(n) calls in a cache to reuse and avoid repeated work.Final Answer:
Use memoization to store and reuse computed values -> Option CQuick Check:
Memoization improves recursion efficiency [OK]
- Thinking increasing recursion limit helps
- Believing loops are only solution
- Removing base cases causing infinite recursion
Ready to prove it?
Pass the test and this pattern moves unknown
