Two Ways to Solve Every DP Problem
Every dynamic programming problem can be solved two ways: top-down with memoization or bottom-up with tabulation. Understanding memoization vs tabulation is not about picking a favorite — it is about knowing which approach leads to a cleaner, faster solution for the specific problem in front of you.
In a coding interview, this choice matters more than you might think. The wrong approach can lead to stack overflow errors, wasted time debugging index math, or a solution that is correct but impossible to optimize further. The right approach gets you to a working answer faster and leaves room to impress the interviewer with follow-up optimizations.
This guide breaks down both approaches side by side, shows you exactly when each one shines, and gives you a concrete interview strategy for deciding between top-down vs bottom-up DP on the spot.
Memoization (Top-Down) Explained
Memoization dynamic programming starts from the original problem and recursively breaks it down into smaller subproblems. Each time you compute a subproblem result, you store it in a cache (usually a hash map or array). When the same subproblem appears again, you return the cached result instead of recomputing it.
The name "top-down" comes from the direction of computation. You start at the top — the full problem — and recurse downward toward the base cases. The recursion tree naturally handles the order of computation for you. You never need to figure out which subproblems to solve first because the call stack takes care of it.
Consider the classic Fibonacci example. A naive recursive solution has O(2^n) time complexity because it recomputes the same values exponentially many times. Adding a memo cache drops this to O(n) — each unique subproblem is computed exactly once, and every subsequent call is an O(1) lookup.
Memoization is particularly natural for problems with complex or multi-dimensional state transitions, tree-shaped subproblem dependencies, and situations where not all subproblems need to be solved. If only a sparse subset of the state space is actually reachable from the original problem, memoization avoids computing the rest entirely.
- Direction: Top-down — start from the original problem, recurse toward base cases
- Data structure: Recursion + hash map or array cache
- Order of computation: Determined automatically by the recursion tree
- Space: O(n) for the cache plus O(n) for the recursion call stack
- Best for: Tree/graph DP, complex state transitions, sparse subproblem spaces
Tabulation (Bottom-Up) Explained
Tabulation dynamic programming works in the opposite direction. You start from the base cases and iteratively build up to the final answer, filling in a table (array) along the way. Each cell in the table represents a subproblem, and you fill cells in an order that guarantees every dependency is already computed.
The name "bottom-up" reflects this: you begin at the bottom (base cases) and work upward to the target problem. There is no recursion involved — just loops and array indexing. This eliminates the function call overhead and, more importantly, eliminates the risk of stack overflow on deep inputs.
Using the Fibonacci example again, tabulation creates an array of size n+1, sets the base cases dp[0]=0 and dp[1]=1, then iterates from 2 to n filling dp[i] = dp[i-1] + dp[i-2]. The result is dp[n]. Same O(n) time complexity as memoization, but with a critical advantage: you can optimize space to O(1) by keeping only the last two values.
This space optimization trick — sometimes called a rolling array — is one of the biggest practical advantages of tabulation dynamic programming. Many 1D and 2D DP problems allow you to reduce space from O(n) or O(n*m) down to O(1) or O(n) by only keeping the rows or values you still need.
- Direction: Bottom-up — start from base cases, build toward the target problem
- Data structure: Iterative loops + table (array or 2D matrix)
- Order of computation: You explicitly define the iteration order
- Space: O(n) for the table, but often reducible with rolling arrays
- Best for: Linear/2D DP, space optimization, large input constraints
Key Insight
Memoization and tabulation have the same time complexity for any given DP problem — the difference is in space usage, stack depth, and coding style.
Memoization vs Tabulation: Side-by-Side Comparison
Both memoization and tabulation solve the same problems with the same time complexity. The differences are in code structure, space usage, stack behavior, and ease of implementation. Here is how they compare across the dimensions that matter most in interviews.
From a code structure perspective, memoization reads like a natural recurrence relation — you write the recursive formula almost verbatim and add a cache. Tabulation requires you to think about iteration order and index boundaries, which can be trickier to get right on a whiteboard. However, tabulation code is often shorter and has no hidden stack overhead.
The space comparison is where tabulation often wins. Memoization uses O(n) for the cache plus O(n) for the call stack (or O(depth) for tree problems). Tabulation uses O(n) for the table but can frequently be optimized to O(1) with rolling variables. For problems like LeetCode 70 (Climbing Stairs) or LeetCode 198 (House Robber), tabulation with space optimization is the gold standard answer.
Stack overflow risk is a real concern with memoization. Languages like Python have a default recursion limit of around 1000, and even languages with larger stacks can overflow on inputs of n=100,000. Tabulation has no such risk because it uses iterative loops. If a problem has large input constraints, recursive vs iterative DP becomes a practical, not just theoretical, concern.
- Time complexity: Identical for both approaches on any given problem
- Space complexity: Memoization uses cache + call stack; tabulation uses table (often optimizable)
- Stack overflow: Memoization risks overflow on deep inputs; tabulation is always safe
- Ease of coding: Memoization maps directly to the recurrence; tabulation requires careful index management
- Space optimization: Only tabulation supports rolling array tricks to reduce space
- Subproblem coverage: Memoization solves only reachable subproblems; tabulation solves all of them
When to Use Memoization in Interviews
Memoization is the right choice when the subproblem structure is complex, non-linear, or sparse. If you are working on a tree DP problem like LeetCode 337 (House Robber III) or a graph DP problem like LeetCode 1463 (Cherry Pickup II), memoization lets you express the solution naturally as a recursive traversal with caching.
Use memoization when the state has three or more dimensions. Problems like LeetCode 312 (Burst Balloons) or LeetCode 1000 (Minimum Cost to Merge Stones) have complex state transitions where figuring out the correct tabulation order is error-prone. Memoization frees you from worrying about iteration order because the recursion handles it automatically.
Memoization also shines when not all subproblems are needed. In some problems, the recursion tree only visits a fraction of the total state space. Tabulation would waste time filling in table entries that never get used. If you suspect the subproblem graph is sparse, memoization can be more efficient in practice even though the worst-case complexity is the same.
Finally, when to use memoization often comes down to speed of implementation. In an interview, getting a correct solution quickly matters more than getting the most optimized solution slowly. If the recurrence relation is clear in your head, writing the memoized version takes two minutes. You can always offer to convert to tabulation as a follow-up.
Pro Tip
In interviews, start with memoization — it maps directly to the recurrence relation and is faster to code. Switch to tabulation only if the interviewer asks for optimization.
When to Use Tabulation in Interviews
Tabulation is the right choice for linear and 2D DP problems where the iteration order is obvious. Problems like LeetCode 322 (Coin Change), LeetCode 300 (Longest Increasing Subsequence), and LeetCode 62 (Unique Paths) have straightforward left-to-right or top-to-bottom iteration patterns that make tabulation clean and easy to code.
Choose tabulation dynamic programming when space optimization is important. Rolling array techniques only work with iterative solutions. If the interviewer asks you to reduce space from O(n^2) to O(n), or from O(n) to O(1), you need tabulation. This is a common follow-up question, and having the tabulation version ready lets you answer it immediately.
Use tabulation when input constraints are large. If n can be up to 10^5 or 10^6, recursive memoization might overflow the stack. Tabulation handles any input size because it only uses heap-allocated arrays and loop variables. For competitive-programming-style problems with tight constraints, tabulation is almost always the safer bet.
Tabulation is also preferred when the problem asks you to fill an entire table that will be queried multiple times. Range DP problems, prefix-sum-based DP, and problems where you need to reconstruct the solution path from the table are all cases where having the complete table available is an advantage.
Interview Strategy: Start Top-Down, Optimize Bottom-Up
Here is the dp interview approach that works in most situations: start with memoization to get a working solution, then convert to tabulation if the interviewer asks for optimization. This two-phase strategy maximizes your chances of both getting the correct answer and impressing with your ability to optimize.
Phase one is about correctness. Write the recurrence relation, add a memo cache, and verify it works on the examples. Memoization maps directly to how you think about the problem recursively, so there is less room for off-by-one errors or incorrect iteration order. In a 45-minute interview, spending 15 minutes on a correct memoized solution is better than spending 30 minutes debugging tabulation index math.
Phase two is about optimization. If the interviewer says "Can you do this without recursion?" or "Can you optimize space?", convert your memoized solution to tabulation. The recurrence relation is already clear from your memoized code, so the conversion is mechanical: replace recursive calls with table lookups, reverse the direction of computation, and add the iteration loops.
Practice both approaches on every DP problem you study on YeetCode. For each problem, solve it first with memoization, then rewrite it as tabulation, and finally apply space optimization. This builds the muscle memory that makes the interview conversion effortless. The best candidates can switch between recursive vs iterative DP fluently, and that flexibility is what earns top marks.
- 1Identify the recurrence relation and base cases from the problem statement
- 2Write the memoized (top-down) solution first — it maps directly to the recurrence
- 3Test it on examples and verify correctness before optimizing
- 4If asked to optimize, convert to tabulation by reversing the computation direction
- 5Apply rolling array or variable optimization to reduce space complexity
- 6Practice both approaches on YeetCode flashcards to build fluency
Watch Out
Recursive memoization can cause stack overflow on deep inputs (e.g., n=10000) — if the problem has large input constraints, tabulation is safer.