CS concepts
Dynamic Programming Patterns Explained: 6 Shapes for Interviews
Updated Nov 27, 2026 · 5 min read
Most interview DP questions are one of six shapes: the last two answers (Climbing Stairs), take or skip (House Robber), a running best (Maximum Subarray), every amount up to the target (Coin Change), grid paths (Unique Paths) and two strings in one table (Longest Common Subsequence). Words like "how many ways", "not adjacent" and "fewest coins" tell you which one.
Key points
- ●DP works because big problems repeat small problems: solve each once, store it, build up.
- ●Every DP answers three questions: what one entry means, the first known answer, and how the next comes from earlier ones.
- ●"How many ways" points to the last two answers; "not adjacent" to take or skip; "contiguous" and "largest" to a running best.
- ●"Fewest coins" points to every amount up to the target; right-or-down grids to above plus left; two strings to a 2D table.
- ●Most traps are at the edges: base cases, all-negative lists, impossible amounts and zeroed grid borders.

Why DP works: big problems repeat small ones
Count the ways up 45 stairs, one or two steps at a time, with plain recursion, and your laptop makes about 3.6 billion function calls. Write each answer down as you go and it makes 45. Same answer either way: 1,836,311,903 ways.
That's dynamic programming. Draw the recursion tree for five stairs and the answer for three stairs gets worked out twice, the answer for two stairs three times. DP solves each small problem once, stores it, and builds up.
Every DP solution answers three questions:
- What does one entry mean?
- What's the first answer you already know?
- How does the next answer come from the ones before it?
You'll answer those three for each of the six patterns below.
Pattern 1: the last two answers
Giveaway: "how many ways" with a small, fixed set of moves.
Climbing Stairs. You climb one or two steps at a time. Your last move came from the stair below or two below, so ways(n) = ways(n - 1) + ways(n - 2). One stair has 1 way, two stairs have 2, then 3, 5, 8. Five stairs, eight ways. You only need the last two numbers, so two variables replace the table. It's Fibonacci in a costume.
Trap: the start. Get a base case off by one and every number after it is wrong. Check one and two stairs by hand.
Pattern 2: take or skip
Giveaway: a rule against neighbors ("not adjacent", "no two in a row") plus "maximum".
House Robber. You can't rob two houses side by side. At each house, either skip it and keep the best from the house before, or take it and add its cash to the best from two houses back. Keep the bigger one. On 2, 7, 9, 3, 1 the best so far goes 2, 7, 11, 11, 12. Houses zero, two and four: 12.
Trap: greed. On 5, 9, 5, grabbing the 9 gives 9, but skipping it gives 10. For House Robber II, where the street is a circle, run the same loop twice: once without the first house, once without the last.
Pattern 3: a running best
Giveaway: "subarray" or "contiguous", together with "largest".
Maximum Subarray. At each number, ask: does the run behind me help, or do I start fresh here? On -2, 1, -3, 4, -1, 2, 1, -5, 4, the run behind the 4 is negative, so drop it. From there the sum goes 4, 3, 5, 6. Best is 6. That's Kadane's algorithm.
Trap: start best at the first number, not zero, or an all-negative list returns a 0 that isn't even in it.
The product twist. For Maximum Product Subarray, a negative times a negative is positive, so the smallest product can become the biggest one step later. Track both. On -2, 3, -4 the smallest run at the 3 is -6, and -6 times -4 is 24.
These first three share a shape: one row of answers, each built from the one or two right before it. The next three look further back.
Pattern 4: every amount up to the target
Giveaway: "fewest coins", "unlimited supply", "make up this amount".
Coin Change. Coins of 1, 2 and 5: what's the fewest coins that make 11? Grabbing the biggest coin first doesn't always work. With coins 1, 3 and 4 and a target of 6, greedy takes 4, 1, 1 (three coins), but 3 plus 3 is two.
So build a table of the fewest coins for every amount from 0 to 11, trying every coin as the last coin. Zero needs 0 coins, 5 needs 1, 6 needs 2, and 11 is 6 plus a 5: 3 coins. That's O(amount × coins), 33 checks here. This is the sheet's Python:
def coin_change(coins, amount):
impossible = amount + 1
dp = [impossible] * (amount + 1)
dp[0] = 0
for current in range(1, amount + 1):
for coin in coins:
if coin <= current:
dp[current] = min(dp[current], dp[current - coin] + 1)
return -1 if dp[amount] == impossible else dp[amount]
Trap: some amounts can't be made. Coins of 2 never make 3. Start every entry at a value that means "impossible", and return -1 if the target is still there at the end. Word Break and Combination Sum IV use the same table with a different question per entry.
Pattern 5: grid paths
Giveaway: a grid where you can only move right or down.
Unique Paths. A robot goes from the top-left to the bottom-right of a 3 by 7 grid. Every cell was reached from above or from the left, so each cell is the cell above plus the cell on its left. The top row and left column are all 1s. Fill in the rest and the corner says 28. One row of storage is enough: before you update a cell it still holds the value from above, so add the one on its left.
Trap: leave the edges at zero and the whole grid fills with zeros. There's a formula too (8 choose 2), but interviewers usually want the table.
Pattern 6: two strings, one table
Giveaway: two strings plus "subsequence" or "common".
Longest Common Subsequence. Put one string across the top and the other down the side. Each cell answers the question for the two prefixes ending there. If the letters match, take the cell diagonally up-left plus one. If not, take the bigger of the cell above and the cell to the left, which means skipping one letter or the other. On "abcde" and "ace", the a, c and e match, and the bottom-right cell says 3.
The extra row and column of zeros stand for the empty string, so you never reach past the edge. It's O(n × m): two 1,000-letter strings make a million cells, which is fine. Edit Distance uses the same table.
The six patterns in one table
| Giveaway words | Pattern | Example | Rule |
|---|---|---|---|
| How many ways, small moves | Last two answers | Climbing Stairs | ways(n-1) + ways(n-2) |
| Not adjacent, maximum | Take or skip | House Robber | max(skip, take + two back) |
| Contiguous, largest | Running best | Maximum Subarray | extend the run or start fresh |
| Fewest coins, unlimited supply | Every amount | Coin Change | 1 + best of (amount - coin) |
| Grid, right or down | Grid paths | Unique Paths | above + left |
| Two strings, subsequence | One 2D table | Longest Common Subsequence | diagonal + 1 on a match |
Next time you see "how many ways" or "fewest", say the three questions out loud: what one entry means, where it starts, and how it builds. The visual sheet above works Coin Change step by step if you want to see the table fill in.
Frequently asked questions
How do I know a problem needs dynamic programming?
Look for a question whose answer can be built from answers to smaller versions of itself, plus giveaway words: "how many ways", "maximum" with "not adjacent", "contiguous", "fewest coins", a right-or-down grid, or two strings with "subsequence" or "common".
What are the three questions every DP solution answers?
What does one entry mean? What's the first answer you already know? And how does the next answer come from the ones before it? Get those right and the loop is short.
Why does greedy fail on Coin Change?
Because the biggest coin first isn't always best. With coins 1, 3 and 4 and a target of 6, greedy takes 4, 1, 1 for three coins, while 3 plus 3 needs only two. DP tries every coin as the last coin for every amount.
Should I write the table or the two-variable version in an interview?
Write the table first. It's easier to explain, and you can shrink it to two variables or one row afterwards, once the interviewer agrees the logic is right.