Memoization vs Tabulation
Choose top-down or bottom-up DP based on clarity, stack depth, reachable states, and iteration dependencies.
What you will be able to do
Memoization and tabulation solve the same recurrence from different directions. Memoization asks only the states it needs. Tabulation fills states in a safe dependency order.
Top-Down and Bottom-Up
Top-down code often looks closer to the recurrence and can skip unreachable states. Bottom-up code avoids recursion depth and can be faster when every state is needed.
Scenario: Recommendation Planning
A recommendation engine explores bundles under constraints. If only a small portion of state space is reachable after business rules, memoization avoids filling irrelevant states. If every budget and item combination is needed for reporting, tabulation is easier to audit.
| Choice | Use when | Risk |
|---|---|---|
| Memoization | Sparse states or recursive clarity | Stack depth and cache key mistakes. |
| Tabulation | All states needed or stack risk | Wrong fill order corrupts answers. |
| Space optimization | Only previous row/state needed | Overwriting dependencies too early. |
| Hybrid | Production constraints need both | More complexity to explain. |
Same Pattern in Java, Python, and JavaScript
House robber is a 1D DP that can be tabulated with two rolling values.
class HouseRobber {
static int rob(int[] nums) {
int twoBack = 0, oneBack = 0;
for (int value : nums) {
int current = Math.max(oneBack, twoBack + value);
twoBack = oneBack;
oneBack = current;
}
return oneBack;
}
}Assessment trap
Bottom-up DP is not automatically better. Explain reachable states, stack depth, fill order, and memory before choosing.
Interview signalFrequently Asked Interview Questions
These are canonical interview patterns for this topic, phrased as concrete problem statements. They avoid vague theory questions and prepare you for the exact reasoning interviewers expect: invariant, edge cases, complexity, and trade-off.
- Solve house robber with bottom-up rolling state.
- Return the fewest coins needed to make an amount.
- Return whether a string can be segmented into dictionary words.
Production practiceCompany-Style Practice Examples
These are not claimed as exact company-specific questions. They are the interview patterns repeatedly used by product companies to test whether you can move from brute force to a defensible implementation.
| Example | Brute-force approach | Stronger solution | Production notes |
|---|---|---|---|
| Sparse recommendation states | Fill a giant table for impossible states. | Use memoization to solve only reachable states. | Sparse business constraints favor top-down. |
| Mobile planning screen | Recursive DP over thousands of states. | Use tabulation to avoid stack depth and improve predictability. | Bottom-up can be easier to profile. |
| Memory-sensitive scoring | Keep full table by default. | Compress to rolling values after proving dependencies. | Premature compression risks correctness bugs. |
Solved modelInterview Question Solutions
Question 1: House Robber
Solve house robber with bottom-up rolling state. Tabulation is compact because only previous two answers are needed.
class HouseRobber {
static int rob(int[] nums) {
int twoBack = 0, oneBack = 0;
for (int value : nums) {
int current = Math.max(oneBack, twoBack + value);
twoBack = oneBack;
oneBack = current;
}
return oneBack;
}
}Question 2: Coin Change
Return the fewest coins needed to make an amount. Bottom-up is natural because all amounts from 0..target are useful.
import java.util.*;
class CoinChange { static int coinChange(int[] coins,int amount){ int[] dp=new int[amount+1]; Arrays.fill(dp, amount+1); dp[0]=0; for(int a=1;a<=amount;a++) for(int c:coins) if(c<=a) dp[a]=Math.min(dp[a],dp[a-c]+1); return dp[amount]>amount?-1:dp[amount]; } }Question 3: Word Break
Return whether a string can be segmented into dictionary words. Memoization is often clear: state is starting index; cache whether suffix can be segmented.
import java.util.*;
class WordBreak { static boolean can(String s, Set<String> dict){ Boolean[] memo=new Boolean[s.length()+1]; return dfs(s,0,dict,memo); } static boolean dfs(String s,int i,Set<String> dict,Boolean[] memo){ if(i==s.length()) return true; if(memo[i]!=null) return memo[i]; for(int end=i+1;end<=s.length();end++) if(dict.contains(s.substring(i,end)) && dfs(s,end,dict,memo)) return memo[i]=true; return memo[i]=false; } }Solved modelWorked Interview Answer
Solve house robber with bottom-up rolling values. The recurrence is best(i) = max(best(i-1), best(i-2) + value). Only the previous two states are needed.
class HouseRobber {
static int rob(int[] nums) {
int twoBack = 0, oneBack = 0;
for (int value : nums) {
int current = Math.max(oneBack, twoBack + value);
twoBack = oneBack;
oneBack = current;
}
return oneBack;
}
}Hands-on drillTry It Yourself
Practice task
Solve house robber with memoization, then convert it to rolling tabulation. Explain what changed and what stayed the same.
Failure patternsCommon Mistakes to Avoid
- Assuming bottom-up is always faster.
- Ignoring recursion depth.
- Filling table in dependency-breaking order.
- Allocating unreachable states unnecessarily.
- Overwriting values needed later.
Execution guardrailQuick-Start Checklist
- Write recurrence first.
- Choose top-down or bottom-up.
- Check reachable state density.
- Check stack depth.
- Define iteration order.
- Consider space compression.
- Explain trade-off.
Recall drillKnowledge Check
| Question | Strong answer |
|---|---|
| When is memoization attractive? | Sparse reachable states or recurrence clarity. |
| When is tabulation attractive? | All states needed or recursion depth is risky. |
| What is stale state overwrite? | Replacing a value before all dependents read it. |
VocabularyKey Terms
| Term | Meaning |
|---|---|
| Top-down | Recursive solving with cache. |
| Bottom-up | Iterative table filling. |
| Reachable state | A state actually needed from the starting problem. |
| Space compression | Reducing table memory by keeping dependency window. |
Next practiceFurther Reading
- Practice coin change, word break, house robber, and edit distance.
- Compare memoization and tabulation for the same recurrence.
- Study iterative dependency ordering carefully.