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.
- 1Break the problem into a smaller copy of itself.
- 2Solve the smallest cases directly, which are the base cases.
- 3Store every answer the first time you compute it.
- 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.
Practise Dynamic Programming Beyond Fibonacci
Watching shows the repeats and solving trains you to spot them.
Everything in the App in One Place
Practice, interviews and tools all start free. Swipe to see the rest.
DSA practice
Problems filed under the pattern that solves them, not a sheet.
352 problemsOpenCompany sets
Practise what one company asks in the week before your round.
463 companiesOpenCoding interview
Choose a role, write in a real editor and answer while you type.
scored reportOpenMock interviews, spoken
You answer out loud and CodeStory follows up on what you said.
32 topicsOpenDatabase round
Answer DBMS the way a panel asks it, with indexes, joins and normal forms.
in Core CSOpenOOP round
Inheritance, polymorphism and the follow-up you never rehearse.
in Core CSOpenSystem design
How real systems are built, from the primitives to the AI stack.
27 chaptersOpenSQL practice
Real schemas, your query, the rows it returns and a clear verdict.
39 questionsOpenAptitude
Quant, logical, verbal, DI and technical, timed like the real test.
280 questionsOpenOnline assessment
Get a clock, marks per question and hidden tests like a company's judge.
37 papersOpenAlgorithm visualiser
Watch it play on your own input, one step at a time.
26 algorithmsOpenAI that helps, not solves
Hints one at a time and AI review before a mentor sees your work.
100 free creditsOpenResume builder
Build it inside the app, next to the work you practised.
in the appOpenProgress and readiness
One score and the axis where you are weakest.
tracked dailyOpen
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 visualiserWhat 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.
- 1CodeStory finds your starting point, from your first base case to interview ready.
- 2CodeStory explains online and campus batches, with timings for your mornings or evenings.
- 3You decide. No pressure, no payment on the call.