Skip to content

Fibonacci From Recursion to DP
Step by Step

Plain recursion solves the same calls again and again. Storing every answer once, in a memo or a table, makes it linear.

Step through every call at your own pace, then share a link to the step where you got lost.

What You Watch While Calls Repeat

The call tree and the table change on every step, and the visualiser tracks them for you.

Recursion feels short until fib(6) makes 25 calls for 7 different values.

CodeStory's visualiser draws the call tree and fills the table as answers stop being recomputed. Code lines stay highlighted beside both. Step forward, step back or let it play.

Try n = 4 first, where a full subtree repeats for the first time. Then switch on memoisation or the table version and compare the calls.

How Recursion Becomes Dynamic Programming

Steps below follow the picture in the same order.

  1. 1Break the problem into a smaller copy of itself.
  2. 2Solve the smallest cases directly, which are the base cases.
  3. 3Store every answer the first time you compute it.
  4. 4Reuse the stored answer instead of solving the same call twice.

Two Ways to Stop Repeated Calls

Both switches live in the visualiser. Flip one on and watch the repeated calls disappear.

  • Memoised recursionSame recursion, but every answer is stored. Repeats become one lookup (green, "cached"), so the tree shrinks to 2n − 1 calls.
  • Bottom-up tableFill dp[0..n] from the left with no recursion, and build every cell from the two before it. You get n + 1 cells, nothing repeated and no call stack.

Fibonacci Inputs Worth Trying

Five presets cover the base cases and the first repeated subtree.

  • fib(6)25 calls for 7 different values.
  • n = 0A base case with one call and no recursion.
  • n = 1Second base case.
  • n = 2First call that recurses.
  • n = 4First time a full subtree, f(2), is computed twice.

Fibonacci Cost in Time and Space

Interviewers ask how memoisation drops exponential time to linear, and the visualiser counts real operations.

Time
O(1.6ⁿ) → O(n)counted from the real calls on your n
Space
O(n)extra memory for the memo table or call stack
Pattern
Recursion & DPSmaller copies get solved once and reused.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

Fibonacci Visualiser FAQs

Open the visualiser and watch the answers play out.

Open the visualiser
What does the Fibonacci visualiser show?

CodeStory's visualiser draws the call tree and fills the table as answers stop being recomputed, on an n you choose. It highlights the line of code being executed, and you can step forward, step back or let it play.

What is the time complexity of Fibonacci with recursion and DP?

Plain recursion takes exponential time, about O(1.6ⁿ), and storing answers brings it down to O(n) time and O(n) space. CodeStory's visualiser counts real operations as it plays, so you watch the drop instead of trusting the formula.

Why is plain Fibonacci recursion slow?

Plain recursion solves the same calls again and again. In the fib(6) preset it makes 25 calls for only 7 different values.

What is the difference between memoisation and a bottom up table?

Memoised recursion keeps the recursion but stores every answer, so the tree shrinks to 2n − 1 calls. Tables skip recursion and fill dp[0..n] from the left with n + 1 cells.

Can I use my own input?

Yes. Type your own n and the visualiser plays recursion, memoisation or the table on it.

Is the visualiser free?

Yes. You start free with 100 AI credits when you sign up and 100 every month, and watching an algorithm costs no credits.

Stuck on Dynamic Programming? Get a Free Callback

Tell CodeStory where DP loses you, and a mentor plans your next steps.

  1. 1CodeStory finds your starting point, from your first base case to interview ready.
  2. 2CodeStory explains online and campus batches, with timings for your mornings or evenings.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.