Dynamic Programming Interview Patterns: A Practical Guide
Dynamic programming becomes manageable when you learn the handful of patterns behind it. A step-by-step method, the main DP families with templates and classic problems, and how to move from recursion to tabulation.
Dynamic programming has a reputation for being the hardest interview topic. It's less mysterious than it looks once you treat it as a handful of repeatable patterns. This guide extends 15 DSA patterns with a deeper DP treatment.
The 5-step method
- Define the state: what does
dp[i](ordp[i][j]) mean? Say it in one sentence. - Write the recurrence: how does the state come from smaller states?
- Base cases: the smallest subproblems.
- Order of computation: which states must be filled first.
- Optimize space: often you only need the previous row or two values.
Start with recursion plus a cache; convert to a table afterwards.
Pattern 1: 1D linear DP
State depends on a few previous positions.
# House Robber: max sum with no two adjacent
def rob(nums):
prev, cur = 0, 0
for x in nums:
prev, cur = cur, max(cur, prev + x)
return curAlso: climbing stairs, decode ways, max subarray (Kadane), longest increasing subsequence.
Pattern 2: Grid DP
dp[r][c] from the cell above and left.
def unique_paths(m, n):
dp = [[1]*n for _ in range(m)]
for r in range(1, m):
for c in range(1, n):
dp[r][c] = dp[r-1][c] + dp[r][c-1]
return dp[-1][-1]Also: minimum path sum, maximal square, dungeon game.
Pattern 3: Knapsack / subset
State: index plus remaining capacity or target.
def can_partition(nums):
total = sum(nums)
if total % 2: return False
target = total // 2
dp = [True] + [False]*target
for x in nums:
for t in range(target, x-1, -1):
dp[t] = dp[t] or dp[t-x]
return dp[target]Also: 0/1 knapsack, coin change (min coins, ways), target sum, partition into subsets.
Pattern 4: Strings (two sequences)
State: dp[i][j] for prefixes of two strings.
def lcs(a, b):
dp = [[0]*(len(b)+1) for _ in range(len(a)+1)]
for i in range(1, len(a)+1):
for j in range(1, len(b)+1):
dp[i][j] = dp[i-1][j-1]+1 if a[i-1]==b[j-1] else max(dp[i-1][j], dp[i][j-1])
return dp[-1][-1]Also: edit distance, longest palindromic subsequence, distinct subsequences, regex/wildcard matching.
Pattern 5: Interval DP
State: a range [i, j], split at k.
Examples: burst balloons, matrix chain multiplication, palindrome partitioning. Hardest tier; learn after the others.
Practice order
| Stage | Problems |
|---|---|
| 1 | Climbing stairs, house robber, min cost climbing stairs |
| 2 | Unique paths, min path sum, coin change |
| 3 | Subset sum, partition equal subset, LIS |
| 4 | LCS, edit distance, word break |
| 5 | Burst balloons, regex matching |
Communicating DP in an interview
State the definition of dp, show the recurrence, trace a small example, give time and space complexity, and mention the space optimization. This framing helps even if you stumble on details. See how to crack a DSA coding round.
Worked example: coin change, from recursion to table
Problem: given coin denominations and a target amount, find the minimum number of coins.
State: best(a) = fewest coins to make amount a. Recurrence: best(a) = 1 + min(best(a - c)) over coins c that do not exceed a. Base case: best(0) = 0; unreachable amounts are infinity.
Recursion with memoization first: call best(amount), cache every result. Then convert to a bottom-up table where you fill amounts from 1 to the target. Time is amount x number of coins; space is amount.
Narrating exactly this progression is what interviewers want to hear.
How to spot the family
| Clue in the problem | Likely pattern |
|---|---|
| "Number of ways to reach step n" | 1D linear |
| "Minimum cost path in a grid" | Grid |
| "Can we pick a subset that sums to X" | Knapsack / subset |
| "Longest common / edit distance between two strings" | Two-sequence string DP |
| "Best way to split or burst a range" | Interval DP |
| "Longest increasing / best ending at i" | 1D with look-back |
Space optimization
Most DP tables depend only on the previous row or the last two values. Keep just those. For 0/1 knapsack, iterate the capacity in reverse so each item is used once. Mention this at the end, because it shows you understand the structure.
Debugging DP
- Print the table for a tiny input and compare with a hand calculation
- Check base cases and indices, especially off-by-one in string DP
- Verify iteration order: dependencies must be computed before use
- Compare against brute force on small random inputs
Common mistakes
- Defining the state too vaguely
- Forgetting to initialize with the right sentinel (infinity versus zero)
- Mixing up "number of ways" (sum) and "minimum" (min) recurrences
- Using a greedy approach where choices interact
A weekly practice routine
| Day | Task |
|---|---|
| 1 | Solve one problem from the family with recursion + memo |
| 2 | Convert it to a table and optimize space |
| 3 | Solve a variant of the same family |
| 4 | Explain one aloud as if in an interview |
| 5 | Mixed review of earlier problems |
Pair this with the broader 15 DSA patterns.
Related reading
Frequently asked questions
Was this article useful?
Bring backup into your next round.
Install InterviewPilot, upload your resume, and walk in with structured answers a keystroke away — visible to you and nobody else.
More from the blog
Python vs Java vs C++ for Coding Interviews: Which Should You Use?
Which language to pick for DSA interviews: speed of writing, standard library strength, performance limits, and what interviewers care about. Practical guidance for Indian placements and product-company loops.
15 DSA Patterns That Solve Most Coding Interview Questions
The 15 data-structure and algorithm patterns behind most LeetCode and HackerRank interview problems — how to recognise each from the problem statement, a minimal Python template, typical complexity, and classic practice questions.
How to Crack a DSA Coding Round: A Practical Framework
A structured approach to data-structures-and-algorithms interviews — how to break down a problem, communicate while you solve it, and avoid the mistakes that sink otherwise-strong candidates.
Discussion
No comments yet. Been through an interview like this? Your experience could help the next candidate.