Fibonacci Using Recursion in DSA Typescript - Time & Space Complexity
We want to understand how long it takes to find a Fibonacci number using recursion.
Specifically, how the time grows as the input number gets bigger.
Analyze the time complexity of the following code snippet.
function fibonacci(n: number): number {
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 numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two recursive calls for each number greater than 1.
- How many times: Each call splits into two more calls until reaching base cases.
Each number requires two smaller Fibonacci calls, 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 doubles with each increase in the input number, 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 calls grow much faster than just counting down.
Understanding this helps you see why naive recursion can be slow and why techniques like memoization improve performance.
"What if we add memoization to this recursive Fibonacci? 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: Recognize recursion use
Recursion calls the function itself to calculate previous two numbers and sums them.Final Answer:
Each number is the sum of the two preceding numbers -> Option AQuick Check:
Fibonacci = sum of two previous numbers [OK]
- Thinking Fibonacci multiplies previous numbers
- Confusing subtraction or squares with addition
- Assuming Fibonacci is a product sequence
Solution
Step 1: Identify input and output types
The Fibonacci function takes a number input and returns a number output.Step 2: Match correct function signature
Only the signature with number input and number output matches the Fibonacci function requirements.Final Answer:
function fib(n: number): number -> Option CQuick Check:
Input and output types must be number [OK]
- Using wrong parameter types like string or boolean
- Missing return type or using void
- Confusing function signature syntax
function fib(n: number): number {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
console.log(fib(4));Solution
Step 1: Calculate fib(4) using recursion
fib(4) = fib(3) + fib(2) - fib(3) = fib(2) + fib(1) = (fib(1) + fib(0)) + 1 = (1 + 0) + 1 = 2 - fib(2) = fib(1) + fib(0) = 1 + 0 = 1 So fib(4) = 2 + 1 = 3Step 2: Confirm output
The console.log prints 5 as the result of fib(4).Final Answer:
5 -> Option AQuick Check:
fib(4) = 5 [OK]
- Confusing fib(4) with fib(5)
- Miscounting base cases fib(0) and fib(1)
- Adding wrong recursive results
function fib(n: number): number {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n - 1) - fib(n - 2);
}Solution
Step 1: Analyze return statement
The function uses subtraction (fib(n - 1) - fib(n - 2)) which is incorrect for Fibonacci.Step 2: Correct the operation
Fibonacci requires addition of previous two numbers, so change '-' to '+'.Final Answer:
Change subtraction to addition in return statement -> Option BQuick Check:
Fibonacci sums previous two numbers [OK]
- Using subtraction instead of addition
- Removing base cases causing infinite recursion
- Changing return type unnecessarily
fib(0) and fib(1)? Why are these base cases important?function fib(n: number): number {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}Solution
Step 1: Evaluate base cases fib(0) and fib(1)
fib(0) returns 0 and fib(1) returns 1 as per the condition n <= 1 returns n.Step 2: Understand importance of base cases
Base cases stop the recursion from continuing infinitely by providing direct answers for smallest inputs.Final Answer:
fib(0) = 0, fib(1) = 1; base cases stop infinite recursion -> Option DQuick Check:
Base cases return 0 and 1 to end recursion [OK]
- Mixing up fib(0) and fib(1) values
- Thinking base cases are optional
- Ignoring recursion stopping condition
Ready to prove it?
Pass the test and this pattern moves unknown
