Top-Down vs Bottom-Up
Step 1 of 12
Top-down starts at the full problem — fib(4) — and recurses only into what it actually needs.
The Idea
Two directions, one recurrence. Top-down starts at the full problem, recurses, and caches whatever it meets — so it touches only the subproblems it actually needs. Bottom-up starts at the base cases and fills a table forward, so every dependency is ready before it is read. Top-down is easier to derive from the recursion; bottom-up avoids deep call stacks.
Real-World Example
Revising for an exam. Top-down means opening the hardest past paper and looking things up only when a question demands them. Bottom-up means working the syllabus chapter by chapter, so nothing is ever missing when you reach the paper — though you also study pages the paper never asks about.
The Code
Your turn
Put the steps in the right order.
- Fill the remaining cells in dependency order
- Decide what a single table cell will mean
- Read the final cell as the answer
- Write the base-case cells in directly
Mini quiz
1 / 3