DSA Notes
Master essential DP interview problems: coin change, edit distance, rod cutting, climbing stairs, and house robber with clear approaches and solutions.
Problem-Solving Framework
For any DP problem in an interview:
- Identify it is DP: Optimal value? Count ways? Has overlapping subproblems?
- Define the state: What changes between subproblems?
- Write the recurrence: How do states relate?
- Determine base cases: What are trivial answers?
- Choose approach: Memoization (easier to code) or tabulation (better space)?
Problem 1: Coin Change (Minimum Coins)
Problem: Given coins of different denominations and a total amount, find the minimum number of coins needed to make that amount. You have infinite supply of each coin.
State: dp[amount] = minimum coins needed to make amount Transition: dp[a] = min(dp[a - coin] + 1) for each valid coin Base: dp[0] = 0 (zero coins for amount 0)
Time: O(amount × len(coins)), Space: O(amount)
Problem 2: Edit Distance (Levenshtein Distance)
Problem: Find minimum operations (insert, delete, replace) to transform word1 into word2.
State: dp[i][j] = edit distance between word1[0..i-1] and word2[0..j-1] Transition:
- Characters match:
dp[i][j] = dp[i-1][j-1] - Replace:
dp[i-1][j-1] + 1 - Delete from word1:
dp[i-1][j] + 1 - Insert into word1:
dp[i][j-1] + 1
Time: O(m×n), Space: O(m×n), optimizable to O(n)
Problem 3: Rod Cutting
Problem: Given a rod of length n and prices for each length, find maximum revenue from cutting.
State: dp[i] = maximum revenue from rod of length i
Time: O(n²), Space: O(n)
Problem 4: Climbing Stairs (Ways to Reach Top)
Problem: You can climb 1 or 2 steps. Count distinct ways to reach step n.
def climb_stairs(n):
if n <= 2: return n
prev2, prev1 = 1, 2
for _ in range(3, n + 1):
curr = prev1 + prev2
prev2, prev1 = prev1, curr
return prev1
print(climb_stairs(5)) # 8This is Fibonacci in disguise! Time: O(n), Space: O(1)
Problem 5: House Robber
Problem: You cannot rob two adjacent houses. Maximize total money robbed.
State: dp[i] = max money from houses 0..i Transition: dp[i] = max(dp[i-1], dp[i-2] + nums[i]) (skip or rob house i)
Time: O(n), Space: O(1)
House Robber II (Circular)
Houses are in a circle (first and last are adjacent). Solution: max(rob houses 0..n-2, rob houses 1..n-1).
Problem 6: Word Break
Problem: Can string s be segmented into dictionary words?
Time: O(n² × k) where k is average word length for substring comparison
Problem 7: Decode Ways
Problem: A message of digits. '1' → A, '2' → B, ..., '26' → Z. Count valid decodings.
Problem 8: Maximum Subarray (Kadane\'s Algorithm)
Problem 9: Unique Paths in Grid
Problem 10: Partition Equal Subset Sum
Problem: Can array be partitioned into two subsets with equal sum?
This is a subset sum problem (special case of 0/1 knapsack).
Pattern Recognition Cheat Sheet
| If the problem asks... | Think... | Pattern |
|---|---|---|
| Min/max with choices | DP with optimal substructure | Knapsack / Interval DP |
| Count ways | DP with additive recurrence | Climbing stairs / Coin change |
| Can it be done? | DP with boolean states | Subset sum / Word break |
| Optimal partitioning | Interval DP | MCM / Palindrome partition |
| Sequence optimization | 1D/2D DP | LIS / LCS / Edit distance |
| Grid paths | 2D DP | Unique paths / Min path sum |
| No adjacent constraint | Linear DP | House robber |
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Dynamic Programming Interview Problems.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, dynamic, programming, interview
Related Data Structures & Algorithms Topics