Dynamic Programming Patterns For Coding Interviews
Asked of: Software Engineer
Last updated

What's being tested
Demonstrates ability to convert a problem into a Dynamic Programming state and derive correct transitions, trading time for reuse. Interviewers probe precise state definition, boundary/base cases, complexity reasoning, and when to switch to greedy/graph search. They expect clear memoization/tabulation choices and space/time optimizations.
Patterns & templates
-
Top‑down memoization — define recursive
dp(state), cache results in a map/array; often easiest to get correctness first. -
Bottom‑up tabulation — build
dp[]ordp[][]iteratively; good for guaranteeingO(n)/O(n^2)time and predictable memory. -
Knapsack / subset DP — 0/1 or unbounded variants using
dp[w]ordp[i][w]; convert choices into weight/value transitions. -
Sequence DP (LIS/LCS) — use
dp[i]ordp[i][j]with clear meaning (best ending at i / best using prefixes i,j); commonO(n^2)baseline. -
Bitmask DP — represent subsets as bitmasks for N ≤ ~20, use
dp[mask]withO(N*2^N)transitions. -
State compression & rolling arrays — drop a dimension when
dp[i]depends only oni-1; convertdp[i][j]toprev[j]to save memory.
Common pitfalls
Pitfall: Defining a state that omits needed context (e.g., forgetting "last taken index") leads to incorrect transitions and overcounting.
Pitfall: Not proving or checking base cases; off‑by‑one in base leads to wrong answers or crashes.
Pitfall: Using naive DP with unnecessary dimensions — causes TLE or MLE when
O(n^3)can be reduced toO(n^2)orO(n).
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Related concepts
- Dynamic Programming Refresher For Product Coding
- Dynamic Programming And MemoizationCoding & Algorithms
- Adobe-style coding patterns: DP, graphs, parsing, backtracking, stacks/heaps
- Dynamic Programming, Scheduling, And Set CoverCoding & Algorithms
- Dynamic Programming, Backtracking, And Combinatorial SearchCoding & Algorithms
- Dynamic Programming And Mutable Range QueriesCoding & Algorithms