Pattern · 35 problems
Dynamic Programming Interview Problems
Dynamic programming is recursion that remembers. I define the state in one sentence, then ask what it depends on.
Free: write the code and run it, no sign-up.
When I reach for it
- Brute force repeats the same subproblem.
- You are asked for a count, a minimum or a maximum.
- The answer for n builds on answers for smaller n.
Problems, easiest first
3 easy · 25 medium · 7 hard. Open one to read it, then write it in the free editor.
- Counting BitseasyBit Manipulation
- Climbing Stairseasy
- Min Cost Climbing StairseasyArrays & Hashing
- Decode WaysmediumStrings
- House RobbermediumArrays & Hashing
- House Robber IImediumArrays & Hashing
- Jump GamemediumGreedyArrays & Hashing
- Longest Common SubsequencemediumStrings
- Longest Increasing SubsequencemediumBinary SearchArrays & Hashing
- Longest Palindromic SubstringmediumTwo PointersStrings
- Maximum Product SubarraymediumArrays & Hashing
- Maximum SubarraymediumArrays & Hashing
- Non-overlapping IntervalsmediumIntervalsGreedy
- Palindromic SubstringsmediumTwo PointersStrings
- Unique Pathsmedium
- Coin ChangemediumBFS & DFSArrays & Hashing
- Word BreakmediumTreesStrings
- Cheapest Flights Within K StopsmediumHeapsGraphs
- Coin Change IImediumArrays & Hashing
- Edit DistancemediumStrings
- Equal Sum Array PartitionmediumArrays & Hashing
- Generate ParenthesesmediumBacktrackingStrings
- Interleaving StringmediumStrings
- Jump Game IImediumGreedyArrays & Hashing
- Palindrome PartitioningmediumBacktrackingStrings
Show the other 10 problems
- Stock Trading with CooldownmediumArrays & Hashing
- Target SummediumBacktrackingArrays & Hashing
- Valid Parenthesis StringmediumStackGreedy
- Binary Tree Maximum Path SumhardTreesBFS & DFS
- Burst BalloonshardArrays & Hashing
- Distinct SubsequenceshardStrings
- Longest Increasing Path in a MatrixhardGraphsBFS & DFS
- Regular Expression MatchinghardBacktrackingStrings
- Trapping Rain WaterhardStackHeaps
- Maximum Profit in Job SchedulinghardBinary SearchArrays & Hashing
The idea
Define the state in one sentence, such as: dp[i] is the best answer using the first i items. Then write how it depends on smaller states.
Start with plain recursion. Add a cache. Then, if you want, turn it into a table. Each step keeps the logic and cuts the cost.
INF = float("inf")
dp = [0] + [INF] * amount # dp[a] = fewest coins to make a
for a in range(1, amount + 1):
for coin in coins:
if coin <= a:
dp[a] = min(dp[a], dp[a - coin] + 1)
return dp[amount] if dp[amount] != INF else -1Where it goes wrong
- A vague state. If you cannot say it in one sentence, redo it.
- Wrong or missing base cases.
- Filling a table in the wrong order. Smaller states must come first.
Say this before you type. Say the state in one sentence, then say which smaller states it reads from.
Where Coco comes in
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit, and to talk with her for one hour. After that, talking comes with the $79 pack. Anyone can open the editor and run their code.
Questions people ask
- Top-down or bottom-up?
- Top-down is easier to get right, because it follows the recursion. Bottom-up avoids recursion limits and can shrink the memory.
- How do I spot a dynamic programming problem?
- Brute force repeats the same subproblems, and a greedy choice can fail. That pairing points to dynamic programming.
Meet these next
Fin and Coco are characters, not people. strongyes.io is independent. Leo builds it with AI agents.
