To excel in technical coding interviews (FAANG, Tier-1 tech firms), you do not need to memorize hundreds of individual dynamic programming problems. Instead, 90% of interview DP problems derive directly from three foundational archetype patterns:
The Problem: Given $N$ items with weights weights[i] and values values[i], and a knapsack of maximum capacity $W$. Find the maximum total value we can pack into the knapsack such that each item can be chosen at most once (0 or 1).
Let dp[i][w] be the maximum value achievable using a subset of the first i items with a remaining weight capacity w.
For each item $i$:
dp[i-1][w]values[i-1] + dp[i-1][w - weights[i-1]]$$\text{dp}[i][w] = \max(\text{dp}[i-1][w], \text{values}[i-1] + \text{dp}[i-1][w - \text{weights}[i-1]])$$
Notice that state dp[w] depends solely on values from the previous row at or to the left of w. By traversing the capacity backwards from $W$ down to $\text{weight}$, we can compress the 2D grid into a single 1D array without overwriting needed prior states:
The Problem (LeetCode 322): Given coins of different denominations and a total amount of money, return the minimum number of coins needed to make up that amount. If that amount cannot be made up, return -1. Each coin is available in unlimited supply.
Let dp[a] be the minimum coins needed to produce amount a.
$$\text{dp}[a] = \min_{c \in \text{coins}, c \le a} (\text{dp}[a - c] + 1)$$
Base case: dp[0] = 0; all other amounts initialized to $\infty$.
[1, 2, 5], Target 11| Amount ($a$) | Calculation | dp[a] | Optimal Combination |
|---|---|---|---|
| 0 | Base Case | 0 | [] |
| 1 | $\text{dp}[1-1]+1 = 0+1$ | 1 | [1] |
| 2 | $\min(\text{dp}[1]+1, \text{dp}[0]+1) = \min(2, 1)$ | 1 | [2] |
| 5 | $\min(\text{dp}[4]+1, \text{dp}[3]+1, \text{dp}[0]+1)$ | 1 | [5] |
| 11 | $\min(\text{dp}[10]+1, \text{dp}[9]+1, \text{dp}[6]+1) = 2+1$ | 3 | [5, 5, 1] |
The Problem (LeetCode 1143): Given two strings text1 and text2, return the length of their longest common subsequence. (A subsequence retains relative order without requiring consecutive adjacency).
Let dp[i][j] be the LCS of text1[0...i-1] and text2[0...j-1].
$$\text{dp}[i][j] = \begin{cases} 1 + \text{dp}[i-1][j-1] & \text{if } text1[i-1] == text2[j-1] \\ \max(\text{dp}[i-1][j], \text{dp}[i][j-1]) & \text{if } text1[i-1] \neq text2[j-1] \end{cases}$$
| LeetCode Problem | Archetype Pattern | Time Complexity | Space Complexity |
|---|---|---|---|
| Partition Equal Subset Sum (416) | 0/1 Knapsack | $O(N \times \text{Target})$ | $O(\text{Target})$ |
| Target Sum (494) | 0/1 Knapsack | $O(N \times \text{Sum})$ | $O(\text{Sum})$ |
| Coin Change II (518) | Unbounded Knapsack (Forward Loop) | $O(N \times \text{Amount})$ | $O(\text{Amount})$ |
| Edit Distance (72) | 2D LCS Variant | $O(M \times N)$ | $O(M \times N)$ |
| Longest Palindromic Subsequence (516) | LCS of String with its Reverse | $O(N^2)$ | $O(N^2)$ |
text1[i-1] == text2[j-1]?| Previous Lesson | Next Lesson |
|---|---|
| Dynamic Programming: Overlapping Subproblems, Memoization vs Tabulation | None |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.