Dynamic programming scares more engineers than any other topic. Most candidates either skip it entirely (and lose offers at FAANG) or grind 100 DP problems without understanding patterns (and still struggle in interviews). This guide fixes both problems.
Why DP Feels Hard
DP feels hard for two reasons:
- The leap from recursive to iterative isn't intuitive โ most people try to write the DP table first instead of starting with recursion + memoization
- There are actually multiple distinct DP patterns โ treating all DP as one thing leads to confusion
The fix: Start with top-down (memoization). Convert to bottom-up (tabulation) after. This sequence works for almost every DP problem.
The Two Approaches to DP
Top-down (memoization): Write a recursive solution, then cache results to avoid recomputation. Bottom-up (tabulation): Build the answer iteratively from base cases.
Both are valid in interviews. Top-down is usually easier to reason about; bottom-up is often more space-efficient.
The 7 Core DP Patterns
Pattern 1: Linear DP (1D state)
State depends on a single index or value.
Fibonacci / Climbing Stairs:
def climbStairs(n):
if n <= 2: return n
dp = [0] * (n + 1)
dp[1], dp[2] = 1, 2
for i in range(3, n + 1):
dp[i] = dp[i-1] + dp[i-2]
return dp[n]
Problems: Climbing Stairs, House Robber, Jump Game, Decode Ways
Pattern 2: 0/1 Knapsack
Given items with weight/value, maximize value within a weight limit. Each item used at most once.
State: dp[i][w] = max value using first i items with capacity w
Problems: 0/1 Knapsack, Partition Equal Subset Sum, Target Sum, Last Stone Weight II
Pattern 3: Unbounded Knapsack
Same as 0/1 but items can be used multiple times.
Problems: Coin Change (minimum coins), Coin Change II (number of ways), Rod Cutting
Pattern 4: Longest Common Subsequence (LCS)
State depends on two sequences.
def longestCommonSubsequence(text1, text2):
m, n = len(text1), len(text2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
if text1[i-1] == text2[j-1]:
dp[i][j] = dp[i-1][j-1] + 1
else:
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
return dp[m][n]
Problems: LCS, Edit Distance, Shortest Common Supersequence, Interleaving String
Pattern 5: Matrix / Grid DP
State is a cell (i, j) in a 2D grid.
Problems: Unique Paths, Minimum Path Sum, Maximal Square, Dungeon Game
Pattern 6: Interval DP
State is a range [i, j]. Usually involves optimal splitting.
State: dp[i][j] = optimal result for the interval from i to j
Problems: Burst Balloons, Matrix Chain Multiplication, Palindrome Partitioning II, Strange Printer
Pattern 7: Tree DP
State is computed recursively on tree nodes.
Problems: House Robber III, Binary Tree Cameras, Maximum Path Sum
How to Recognize a DP Problem
DP applies when:
- The problem asks for optimal (max, min, longest, shortest, fewest)
- The problem asks to count the number of ways to do something
- Decisions at each step affect future decisions
- Naive recursion would recompute the same subproblems
If you see these words in a problem: maximum, minimum, longest, shortest, number of ways, count, can we achieve โ immediately think DP.
The 5-Step DP Solving Process
- Identify the state: What variables uniquely define a subproblem?
- Write the recurrence: How does
dp[i]relate todp[i-1](ordp[i][j]to neighbors)? - Identify base cases: What are the trivially known values?
- Decide order: Which subproblems must be solved before others?
- Extract the answer: Where in the DP table is the final answer?
The DP Problems Every Engineer Should Know
Essential (Blind 75 + NeetCode 150):
- Climbing Stairs
- Coin Change
- Longest Increasing Subsequence
- Longest Common Subsequence
- Edit Distance
- Word Break
- Combination Sum IV
- House Robber / House Robber II
- Decode Ways
- Unique Paths
- Jump Game / Jump Game II
- Partition Equal Subset Sum
Hard (FAANG-level):
- Burst Balloons
- Regular Expression Matching
- Wildcard Matching
- Maximal Rectangle
- Distinct Subsequences
- Interleaving String
Practice Strategy for DP
- Do Pattern 1 (linear DP) problems until they're mechanical โ these are the foundation
- Move to 0/1 knapsack โ understand the
dp[i][w]state deeply - Do LCS variants โ 2D DP on strings is very common at FAANG
- When you get a new DP problem in practice, identify the pattern first before solving
- Re-solve problems you got wrong 3 days later without looking at the solution
The goal is pattern recognition, not memorization. Use CareerLift.ai to practice walking through your DP reasoning out loud โ explaining your state definition and recurrence to an interviewer is a learnable skill.