Memoization vs Tabulation
Two ways to cache subproblem answers.
Memoization vs Tabulation is a free Competitive Programming Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Competitive Programming Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Cache at All
Naive recursion redoes the same work again and again. Dynamic programming stores each answer once so you never recompute it.
fib(40) # slow: recomputes endlesslyOverlapping Subproblems
DP applies when a problem splits into overlapping subproblems. The same smaller case shows up across many branches of the recursion.
fib(5) needs fib(3) twiceTop-Down: Memoization
Memoization is plain recursion plus a cache. You compute on demand and remember the result the first time you see each input.
memo = {}Easy Memo in Python
The lru_cache decorator turns slow recursion into fast DP with one line, caching every call automatically.
from functools import lru_cache
@lru_cache(None)
def f(n): ...Bottom-Up: Tabulation
Tabulation fills a table from the smallest cases up to the answer, using a loop instead of recursion.
dp = [0] * (n + 1)A Tabulated Fibonacci
Set the base values, then let each cell read the ones already computed. No call stack, just a clean loop.
dp[0], dp[1] = 0, 1
for i in range(2, n+1):
dp[i] = dp[i-1] + dp[i-2]Same Answer, Different Style
Memoization and tabulation solve the same recurrence. They differ only in direction: top-down on demand, or bottom-up in order.
When to Prefer Memoization
Reach for memoization when the recurrence is natural to write and you may not need every state.
When to Prefer Tabulation
Choose tabulation for tight loops, to dodge recursion-limit errors, and when you will compute the whole table anyway.
import sys; sys.setrecursionlimit(10**6)Watch the Recursion Limit
Deep memoized recursion can hit Python's recursion limit and crash with a runtime error verdict on large inputs.
Both Share One Cost
Either way, the speedup comes from solving each state once. Total time is the number of states times the work per state.
Quick Check
Which approach fills a table bottom-up with a loop?
Recap: Two Roads, One DP
You can now cache subproblems two ways. Memoization recurses top-down; tabulation loops bottom-up. Pick whichever reads cleaner. ✨
Frequently asked questions
Is the “Memoization vs Tabulation” lesson free?
Yes — the full text of “Memoization vs Tabulation” is free to read here on the web, and the Competitive Programming Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Competitive Programming Academy course, upgrade to CoddyKit PRO.
What will I learn in “Memoization vs Tabulation”?
Two ways to cache subproblem answers. You practise Competitive Programming Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Competitive Programming Academy?
No prior experience is required. Competitive Programming Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Memoization vs Tabulation” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Competitive Programming Academy lesson?
Yes. Every Competitive Programming Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Memoization vs Tabulation
- Define State and Transition
- Climbing Stairs & Coin Combinations
- Longest Increasing Subsequence