1. Base cases
F(0) and F(1) are already known.
if (n <= 1) return nTyporithm · easy
Step 1 of 8 · intro card
Dynamic Programming
Fibonacci DP stores earlier Fibonacci values so each state is computed once.
The naive recursive version repeats the same calls many times.
Tabulation builds from known base cases upward.
Each new state reads only the two previous states.
State: dp[i] is F(i).
Base cases: F(0)=0, F(1)=1.
Transition: dp[i-1] + dp[i-2].
Time O(n), space O(n).
function fibonacciDp(n) {
if (n <= 1) return n;
const dp = Array(n + 1).fill(0);
dp[1] = 1;
for (let i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}Connect each part of the code to the concept before you type.
F(0) and F(1) are already known.
if (n <= 1) return ndp[i] stores the ith Fibonacci value.
const dp = Array(n + 1).fill(0)Initialize the first non-zero Fibonacci value.
dp[1] = 1Each value is the sum of the two previous values.
dp[i] = dp[i - 1] + dp[i - 2]Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
Inputn = 6
Follow temporary values, local state and supporting structures at this step.
↗ The 7-cell working table above is itself auxiliary memory. Input values are shown separately.
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state