Dynamic Programming Patterns: Coding Interview Guide

DP is recognition: identify the state, the transition, and the order. The rest is implementation. Used in 9+ canonical interview problems and appears regularly in onsite loops at 5 top companies.

When To Use the Dynamic Programming Pattern

Pattern recognition is the meta-skill in coding interviews. Dynamic Programming is the right tool in the following situations, and learning to spot them in under 30 seconds is how strong candidates avoid the brute-force trap.

  • Optimal substructure: solution decomposes into smaller solutions
  • Overlapping subproblems: same subproblem appears multiple times in naive recursion
  • Counting paths or ways with constraints
  • Min/max optimization with choices at each step
  • Sequence alignment, edit distance, longest common subsequence

If you see two or more of these signals in the problem statement, default to dynamic programming as your first approach. If the brute-force pass doesn't fit, this is usually the next stop.

Template Code

Memorize this skeleton. In real interviews you should be able to type it from muscle memory in under two minutes, then specialize the inner logic for the specific problem. The bug surface in dynamic programming is usually in the inner conditional, not the outer loop, so getting the boilerplate right protects you from off-by-one mistakes.

def coin_change(coins, amount):
    dp = [float('inf')] * (amount + 1)
    dp[0] = 0
    for a in range(1, amount + 1):
        for c in coins:
            if c <= a:
                dp[a] = min(dp[a], dp[a - c] + 1)
    return dp[amount] if dp[amount] != float('inf') else -1

This template is the Python form. The same shape translates directly to Java, C++, Go, and JavaScript; only the container types change. Practicing the template in your strongest language first builds the right mental model before you port it.

Time and Space Complexity

  • Time: Depends on state space: 1D O(n*k), 2D O(n*m*k), etc.
  • Space: Often reducible from 2D to 1D rolling array

State the complexity out loud at the start of every problem you solve with this pattern. Interviewers grade on whether you reason about complexity before writing code, not after.

Classic Problems To Practice

Drill these in order. The first 2-3 establish the template, the middle ones add a twist (duplicates, multiple constraints, edge cases), and the last ones combine dynamic programming with a second pattern. Working all of them is roughly 8-12 hours of focused practice.

  • Climbing Stairs
  • House Robber I/II/III
  • Coin Change I/II
  • Longest Common Subsequence
  • Edit Distance
  • Word Break
  • Best Time to Buy and Sell Stock (all variants)
  • Longest Increasing Subsequence
  • Partition Equal Subset Sum

After finishing the list, do a timed mock with one problem from this pattern picked at random. If you can solve it in under 25 minutes with no hints and a clean complexity analysis, you have internalized the pattern.

Companies That Ask the Dynamic Programming Pattern

Based on LeakCode's aggregated interview reports, dynamic programming questions appear regularly in coding rounds at the following companies. Click any to see their full question pool.

Patterns are not company-specific in theory but in practice some companies lean harder than others. Meta and Amazon are known for two-pointer and sliding-window heavy phone screens; Google leans graph and DP; Stripe favors design-leaning patterns. Calibrate your prep mix by checking the recent reports for your target company before drilling generic lists.

Common Mistakes

These are the failure modes that show up most in LeakCode reports from candidates who got "no hire" or "weak hire" on rounds where this pattern was expected.

  • Off-by-one on dp array size (dp[amount] needs amount+1 entries)
  • Wrong iteration order (rolling 1D requires reverse order for 0/1 knapsack)
  • Treating 0/1 knapsack as unbounded or vice versa
  • Memoizing on too many state variables when the problem is 1D

Read each mistake, then write the template again from scratch making sure you handle the corresponding edge case. The mistakes are easier to internalize when you have made them once in a low-stakes context than when you discover them in a real interview.

How To Practice This Pattern

The fastest way to internalize dynamic programming is deliberate practice with constraint, not raw volume. Three sessions of 90 minutes each beat one session of 6 hours because spaced practice consolidates the pattern recognition wiring.

Session 1: read the template, draw the state diagram on paper, then solve the first 3 classic problems with the template visible. Goal is template fluency, not problem solving.

Session 2: solve problems 4-7 without looking at the template. If you blank, look, then close and restart from scratch. Goal is unaided recall of the template under no time pressure.

Session 3: timed mock with the remaining problems on a 25-minute clock each. Talk through complexity out loud as you would in an interview. Goal is performance under interview tempo. After this session, the pattern should feel obvious when you see a matching problem.

Practice This Pattern on Real Interview Reports

LeakCode has thousands of reports tagged by pattern, company, and round. Filter for reports mentioning dynamic programming at your target company to see exactly how the pattern appeared in recent loops.

Related Coding Patterns