Skip to main content
Dynamic ProgrammingFrameworks

A 4-Step Framework for Every DP Problem

· 8 min read

Syed Peera Saheb

Software Engineer · 5+ years in tech interviews

Summary

Dynamic programming looks hard until you realise 90% of interview DP problems follow the same four-step breakdown. Here is the framework.

Step 1 — Define the state

dp[i] means "the answer for the subproblem ending at / of size i". Define it in plain English before writing a single line of code. Ambiguous state definitions cause wrong recurrences. For 2D problems: dp[i][j] = "the answer considering the first i items of list A and first j items of list B".

Step 2 — Write the recurrence

How does dp[i] relate to smaller subproblems? This is the heart of DP. For Coin Change: dp[i] = min(dp[i - coin] + 1) for all valid coins. For LCS: dp[i][j] = dp[i-1][j-1] + 1 if match, else max(dp[i-1][j], dp[i][j-1]). If you cannot express dp[i] using only earlier values, your state definition is wrong — go back to Step 1.

Step 3 — Set the base cases

Base cases are the stopping conditions. They are usually the smallest valid inputs: dp[0] = 0, dp[1] = 1, or dp[i][0] = i for all i. Missing base cases cause index-out-of-bounds bugs or incorrect results that are hard to trace.

Step 4 — Optimize space if possible

Many 1D DP arrays can be reduced to two variables (curr and prev). Many 2D arrays can be reduced to a single rolling row. Only do this after the correct solution is working — premature space optimization is the source of many DP bugs in interviews.

Frequently Asked Questions

What is dynamic programming in simple terms?
Dynamic programming (DP) solves problems by breaking them into overlapping subproblems and storing results to avoid recomputation. If you have solved dp[i] before, you look it up instead of recalculating. It applies when a problem has optimal substructure (optimal solution uses optimal subproblems) and overlapping subproblems (same sub-problems appear repeatedly).
How do I know when to use dynamic programming?
Use DP when the problem asks for a count, minimum, or maximum and involves making choices at each step. Key signals: "number of ways to...", "minimum cost to...", "maximum value if...", overlapping subproblems (solving the same sub-case repeatedly in recursion), and optimal substructure (the best answer uses the best answer to subproblems).
What is the difference between memoization and tabulation?
Memoization is top-down DP: write the recursive solution, add a cache (dictionary) to store results. Tabulation is bottom-up DP: fill a table iteratively from base cases up. Memoization is easier to write and only computes needed states. Tabulation uses less memory (no recursion stack) and is faster in practice. Both have the same time complexity.
What are the most important DP patterns for coding interviews?
The core DP patterns are: 1D linear DP (Climbing Stairs, House Robber), unbounded knapsack (Coin Change), 0/1 knapsack (Partition Equal Subset Sum), 2D string DP (LCS, Edit Distance), interval DP (Burst Balloons), and state machine DP (Stock Buy/Sell problems). Mastering these 6 patterns covers 90% of interview DP problems.
Is dynamic programming hard to learn?
DP has a steep initial learning curve because every problem requires defining the state — there is no single fixed template. The breakthrough comes when you recognize archetypes: "this is a knapsack variant", "this is LCS-style". Once you have seen 20-30 DP problems across these archetypes, pattern recognition kicks in and DP becomes manageable.

Practice this pattern

See all problems and the code template →

Study pattern

Syed Peera Saheb

Software Engineer · 5+ years · ServiceNow

Software engineer with hands-on experience passing technical interviews at top tech companies. Built Coding Prep Guide to share the pattern-first prep strategy that actually works. Writes about DSA, system design, and interview strategy.

Buy me a coffee