Fibonacci Using DP in DSA Javascript - Time & Space Complexity
We want to understand how fast the Fibonacci sequence calculation runs when using dynamic programming.
How does the time needed grow as we ask for bigger Fibonacci numbers?
Analyze the time complexity of the following code snippet.
function fibonacci(n) {
const dp = [0, 1];
for (let i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
This code calculates the nth Fibonacci number by storing previous results to avoid repeated work.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs from 2 up to n.
- How many times: Exactly n - 1 times, once for each Fibonacci number from 2 to n.
Each new Fibonacci number requires one addition operation using two stored values.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 8 additions |
| 100 | 98 additions |
| 1000 | 998 additions |
Pattern observation: The number of operations grows directly with n, so doubling n roughly doubles the work.
Time Complexity: O(n)
This means the time to find the nth Fibonacci number grows in a straight line as n gets bigger.
[X] Wrong: "Calculating Fibonacci with recursion is always fast because it just calls itself."
[OK] Correct: Simple recursion repeats many calculations, making it much slower than this dynamic programming approach.
Understanding how dynamic programming improves Fibonacci calculation shows you can spot and fix repeated work, a key skill in many coding problems.
"What if we used recursion with memoization instead of a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand recursion vs DP
Simple recursion recalculates the same Fibonacci numbers many times, causing inefficiency.Step 2: Role of DP in Fibonacci
DP stores previously calculated Fibonacci values in a table (memoization or tabulation) to reuse them, avoiding repeated work.Final Answer:
It stores intermediate results to avoid repeated calculations -> Option CQuick Check:
DP = stores results to save time [OK]
- Thinking DP uses less memory than recursion
- Believing DP does not use loops or arrays
- Confusing DP with simple recursion
Solution
Step 1: Array size for DP
We need an array of size n+1 to store Fibonacci numbers from 0 to n.Step 2: Initial values for Fibonacci
Fibonacci sequence starts with fib[0] = 0 and fib[1] = 1, so these must be set correctly.Final Answer:
const fib = new Array(n+1).fill(0); fib[0] = 0; fib[1] = 1; -> Option AQuick Check:
Array size n+1, fib[0]=0, fib[1]=1 [OK]
- Using array size n instead of n+1
- Setting fib[0] = 1 incorrectly
- Filling array with 1 instead of 0
function fibDP(n) {
const fib = [0, 1];
for (let i = 2; i <= n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib[n];
}
console.log(fibDP(6));Solution
Step 1: Trace Fibonacci values up to n=6
fib[0]=0, fib[1]=1, fib[2]=1, fib[3]=2, fib[4]=3, fib[5]=5, fib[6]=8.Step 2: Return fib[6]
The function returns fib[6], which is 8.Final Answer:
8 -> Option AQuick Check:
fib(6) = 8 [OK]
- Confusing index and value (e.g., fib[6] = 13)
- Starting array with wrong initial values
- Off-by-one errors in loop
function fibDP(n) {
const fib = [0, 1];
for (let i = 1; i <= n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib[n];
}Solution
Step 1: Analyze loop start index
The loop starts at i=1, so fib[i-2] becomes fib[-1] which is undefined and causes error.Step 2: Correct loop start
Starting loop at i=2 ensures fib[i-2] is fib[0], which is defined, fixing the error.Final Answer:
Change loop start to i = 2 to avoid undefined fib[-1] -> Option DQuick Check:
Loop from 2 avoids negative index [OK]
- Starting loop at 1 causing undefined access
- Changing return index incorrectly
- Removing DP and using recursion unnecessarily
fibDP(0) and fibDP(1)? How does the function handle these edge cases?function fibDP(n) {
if (n <= 1) return n;
const fib = new Array(n + 1).fill(0);
fib[1] = 1;
for (let i = 2; i <= n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib[n];
}Solution
Step 1: Check base case handling
The function returns n directly if n ≤ 1, so fibDP(0) returns 0 and fibDP(1) returns 1 immediately.Step 2: Confirm array initialization and loop
For n > 1, the array is initialized and filled correctly, but for n=0 or 1, the function returns early, handling edge cases properly.Final Answer:
fibDP(0) = 0, fibDP(1) = 1; returns n directly for n ≤ 1 -> Option BQuick Check:
Edge cases return n directly [OK]
- Not handling n=0 or n=1 separately
- Incorrect array initialization for small n
- Assuming loop runs for n=0 or n=1
Ready to prove it?
Pass the test and this pattern moves unknown
