Dynamic Programming Interview Questions · 2026

Dynamic Programming Interview Questions: From Brute Force to Bottom-Up

MIT's introductory algorithms course, 6.006, spends four of its roughly twenty-four lectures on dynamic programming alone, more time than it gives to graphs, hashing, or sorting individually, according to the course's own MIT OpenCourseWare lecture notes. That's not a scheduling accident. If a syllabus built by people who teach algorithms for a living gives one topic that much runway, interviewers tend to rate it just as high.

Dynamic programming interview questions have a reputation for being the round where strong engineers freeze. The reputation is earned, but it's fixable. Most DP questions interviewers ask are variations on five or six recurring shapes. Once you've drilled the shape instead of the specific problem, new variations stop feeling new, and the twenty minutes you'd otherwise spend stuck at a blank recursion tree gets spent writing code instead, especially once you've stress-tested a few of these shapes out loud in a mock interview.

What dynamic programming interview questions are actually testing

Dynamic programming is a technique for solving a problem by breaking it into smaller versions of itself and reusing the answers you've already worked out, instead of recomputing them from scratch. Two properties have to be true before the technique even applies. The problem needs optimal substructure, meaning the best answer to the whole problem is built from the best answers to its pieces. And it needs overlapping subproblems, meaning a plain recursive solution ends up solving the exact same smaller problem many times over. MIT's Erik Demaine covers both properties directly in the department's dynamic programming lectures, walking through longest common subsequence and longest increasing subsequence as the worked examples for lecture two of the series.

In practice, it's often faster to write the brute-force recursion first and watch where it repeats a call with identical arguments, rather than proving the property before writing any code. If two calls land on the exact same subproblem, you've got overlapping subproblems, and you're already halfway to a DP solution. The tables doing the caching here aren't exotic; they're the same handful of data structures that show up everywhere else in a coding round. Our guide to the data structures that actually get asked about covers the rest of that list if arrays and hash maps still feel shaky. If you'd rather test that against a quick drill than read another list, our free interview tools cover a few of the fundamentals too.

How to spot a DP problem before you start coding

The fastest tell is the question itself. If a problem asks for the minimum, the maximum, the total count of ways to do something, or whether something is even possible, and the answer depends on a sequence of choices where earlier choices constrain later ones, you're probably looking at dynamic programming interview questions in disguise.

A few more signals worth checking, roughly in the order I'd check them:

  • The brute-force solution is recursive, and different call paths land on identical arguments. That's the overlapping-subproblems tell from the section above.
  • The problem can be described as "the best answer at step i depends on the best answer at some earlier step." Fibonacci, climbing stairs, and house robber all fit this shape.
  • There's an explicit constraint like a weight limit, a target sum, or a string length, and the question is really asking you to search a huge space of combinations efficiently.
  • A greedy, take-the-locally-best-option approach gives the wrong answer on at least one input. Coin change with denominations like 1, 3, 4 is the classic counterexample: greedily grabbing the 4 leaves you needing three more 1-coins for a target of 7, when 3 + 4 gets there in two coins.

That last point matters more than most prep guides admit. A lot of candidates default to greedy because it's simpler to code, then can't explain why it fails on the interviewer's follow-up input. If you can't prove a greedy choice is always safe, assume it isn't and reach for DP instead. For patterns that live outside dynamic programming, sliding window, two pointers, binary search on the answer, our breakdown of the fifteen most common coding interview patterns covers where DP sits relative to everything else you'll be asked.

Memoization vs tabulation: same idea, opposite direction

Both techniques solve the identical set of subproblems. The difference is direction and mechanics, not correctness.

Memoization (top-down)Tabulation (bottom-up)
Starts from the original problem and recurses down, caching each result the first time it's computedStarts from the smallest subproblems and builds up to the original problem in a loop
Usually a small, direct rewrite of the brute-force recursion, easy to derive under time pressureRequires deciding the iteration order up front, which is the harder mental step
Only computes subproblems the input actually needs, useful when the state space is sparseComputes every subproblem in the table, even ones the final answer doesn't touch
Recursion depth can blow the call stack on large inputsIterative, so no stack depth risk, and usually a bit faster in practice
Space is whatever the cache needs, hard to shrink furtherOften reducible to O(1) or O(n) extra space once you notice you only ever look back one or two rows

Most interviewers are fine with a correct memoized solution as your first pass. The follow-up, almost every time, is "can you do this without recursion." That's your cue to walk it into a tabulated version, and the space-optimization trick in the last row is usually what separates a pass from a strong pass. I'd rather see a candidate ship a working memoized solution than a broken tabulated one, and I think most interviewers quietly agree, even though the rubric on paper rewards bottom-up more.

Watching one problem go from brute force to tabulated

The examples below are in Python, and there's a reason beyond convenience. Python usage among professional developers jumped to 57.9% in 2025, up seven points from the year before, per Stack Overflow's 2025 Developer Survey. If you get a language choice, Python is the safe default, and it stays readable at every stage of the transformation below.

House Robber is a good problem for this walkthrough because the recursion is small enough to hold in your head, but the transformation covers everything you'll actually reuse on harder DP questions. The setup: houses in a row, each holding a certain amount of cash, and you can't rob two adjacent houses. Maximize the total.

Brute force just tries both choices at every house, rob it or skip it, and recurses:

def rob_brute(houses, i):
  if i < 0:
    return 0
  skip = rob_brute(houses, i - 1)
  take = rob_brute(houses, i - 2) + houses[i]
  return max(skip, take)

# call with rob_brute(houses, len(houses) - 1)

This works, and it's also exponential, because rob_brute(houses, 3) gets called separately from two different paths through the tree, over and over, as i grows. That repetition is the overlapping-subproblems signal from earlier in this post. Add a cache and you've got memoization:

def rob_memo(houses, i, cache={}):
  if i < 0:
    return 0
  if i in cache:
    return cache[i]
  skip = rob_memo(houses, i - 1, cache)
  take = rob_memo(houses, i - 2, cache) + houses[i]
  cache[i] = max(skip, take)
  return cache[i]

Same logic, same recursion, but now each value of i only gets computed once. That alone usually takes House Robber from exponential to linear time. Flip it into tabulation by building the same values bottom-up instead of top-down, and you can go one step further and drop the array entirely, since each step only ever looks back two positions:

def rob_tabulated(houses):
  prev2, prev1 = 0, 0
  for amount in houses:
    prev2, prev1 = prev1, max(prev1, prev2 + amount)
  return prev1

Three versions, same answer, wildly different footprints: exponential time with no extra space, linear time with linear space, and linear time with constant space. Interviewers who ask you to "optimize the space" after you've already got a working solution are asking for exactly this last step, and it's a fast one once you see that the DP only ever needs the last two results.

The recurring DP patterns interviewers keep reusing

Almost every dynamic programming interview question on file falls into one of six shapes. Learn the shape, not the specific problem, and new variations stop being scary. Six is a convenient number, not a hard law; subset sum, for one, sits right on the boundary between two of these categories, and different guides file it differently.

PatternRecognize it byClassic example
Fibonacci-styleAnswer at step i depends on one or two previous stepsClimbing stairs, house robber
0/1 knapsackEach item can be used once, optimizing under a weight or capacity limitPartition equal subset sum, target sum
Unbounded knapsackEach item can be reused any number of timesCoin change (minimum coins, or number of ways)
Longest common subsequenceComparing two sequences and building a 2D table of matchesEdit distance, LCS itself
Longest increasing subsequenceFinding the longest run through one sequence that keeps a propertyLIS, Russian doll envelopes
Grid or matrix DPMoving through a 2D grid where each cell depends on the cell above and to the leftUnique paths, minimum path sum

0/1 knapsack and unbounded knapsack get confused constantly, and it's worth being explicit about the difference out loud in an interview, because it signals you understand the state transition rather than pattern-matching off a memorized template. In 0/1 knapsack, once you've decided to use item i, it's gone. In the tabulated version, that means you iterate the weight dimension in decreasing order so you don't accidentally reuse the same item twice in one pass. In unbounded knapsack, the item stays available, so you iterate weight in increasing order on purpose, letting the same item get picked again within the same row.

Longest increasing subsequence has a well-known O(n log n) version using patience sorting and binary search, on top of the more intuitive O(n squared) table version. I wouldn't spend interview prep time memorizing the log-n trick unless you're already comfortable with the O(n squared) table and you're interviewing for a senior or staff role where it's expected. For most SDE I and SDE II loops, the table version, explained clearly, is enough.

Sample dynamic programming interview questions and answers

What is dynamic programming, in plain terms?

Dynamic programming is solving a problem by breaking it into overlapping smaller problems, solving each one exactly once, and reusing the stored answer every time it comes up again. It only works when a problem has optimal substructure (the best full answer is built from the best answers to its pieces) and overlapping subproblems (plain recursion would solve the same smaller problem repeatedly). Without both properties, DP doesn't apply and you should reach for a different technique.

Should you start with memoization or tabulation in an interview?

Start with memoization. It's a smaller, more mechanical step away from the brute-force recursion you'd write anyway, and it's easier to get right under time pressure. Convert to tabulation afterward if the interviewer asks for it or you have time left, since bottom-up usually reads as the stronger final answer and opens the door to space optimization.

How do you solve the 0/1 knapsack problem?

Build a table where dp[i][w] is the best value achievable using the first i items with capacity w. For each item, you either skip it (dp[i-1][w]) or take it if it fits (dp[i-1][w - weight] + value), keeping the larger of the two. The answer sits at dp[n][capacity]. Because each item can only be used once, the space-optimized version has to iterate the capacity dimension backward, from high to low, so an item doesn't get counted twice in the same pass.

How is unbounded knapsack different from 0/1 knapsack?

In unbounded knapsack, you can reuse the same item as many times as it fits, which is the setup behind coin change. The recurrence looks almost identical to 0/1 knapsack, but the space-optimized loop runs the capacity dimension forward instead of backward, because reusing values computed earlier in the same pass is exactly what you want when an item is allowed to repeat.

How do you find the longest common subsequence of two strings?

Build a 2D table where dp[i][j] holds the length of the longest common subsequence between the first i characters of one string and the first j characters of the other. If the characters at positions i and j match, dp[i][j] equals dp[i-1][j-1] plus one. If they don't, dp[i][j] is the larger of dp[i-1][j] and dp[i][j-1]. Edit distance uses almost the same table, just with a different rule at the mismatch case, which is why interviewers like asking both in the same loop.

How do you find the longest increasing subsequence?

The O(n squared) approach defines dp[i] as the length of the longest increasing subsequence ending at index i, then checks every earlier index j to see if that value is smaller, updating dp[i] to dp[j] plus one when it is. The answer is the largest value across the whole dp array. A faster O(n log n) version exists using binary search over a running "tails" array, but most interviewers accept the table version if you can explain it cleanly.

Why do climbing stairs and house robber count as fibonacci-style DP?

Both problems define the answer at step i purely in terms of the answer at one or two earlier steps, the same recurrence shape as the Fibonacci sequence itself. Climbing stairs is ways(i) = ways(i-1) + ways(i-2). House robber is max(rob up to i-1, rob up to i-2 plus the current house). Once you recognize the shape, both problems collapse to the same three-line tabulation loop with two rolling variables instead of a full array.

How do you approach grid or matrix DP problems?

Define dp[row][col] as the answer for reaching that cell, where the recurrence usually pulls from the cell above and the cell to the left (sometimes diagonal too, depending on the allowed moves). Unique Paths counts the number of ways to reach the bottom-right corner moving only right or down, so dp[row][col] equals dp[row-1][col] plus dp[row][col-1]. Minimum Path Sum swaps the sum for a min. Once you've drawn the grid on the whiteboard and marked the base row and column, most grid DP problems write themselves.

None of this removes the pressure of watching a brute-force solution time out with an interviewer staring at your screen. That moment, where you know the answer needs caching but haven't written it yet, is exactly where LastRound AI's interview copilot is built to help during a live round: it listens to the question as it's asked and returns a structured solution outline in under 200 milliseconds, across fifty-plus languages, without appearing on a shared screen. Getting asked a DP question at all still depends on landing the interview first, and if that part of the pipeline is thin, our auto-apply tool is worth pointing at more roles while you drill the six shapes above. Dynamic programming rarely shows up alone, either; it's usually one question inside a longer loop that also covers system design and behavioral rounds, the way Google's L4 interview process stacks a DP-flavored round next to everything else in the same day. Drill the six shapes, practice the brute-force-to-tabulated conversion until it's automatic, and the next unfamiliar problem probably looks more familiar than it first appears.

How this list was built

Worth being straight about where these questions come from, because plenty of pages in this category are not. The set was compiled from a research pass across official documentation, vendor release notes, published engineering writing and public discussion of hiring processes, then cross-checked against the current version of each technology so nothing here describes behaviour that has since changed.

What that means in practice: these are the questions the material supports as reasonable and current for this role, not a transcript of any one company's loop. We have not sat in on your interview and we are not going to claim we have. Treat the list as well-sourced preparation rather than a leaked question bank, and expect your panel to phrase things their own way.

If you spot something out of date, tell us at contact@lastroundai.com and we will fix it.

Frequently asked questions

Do I need hands-on dynamic programming experience to pass?

It shows quickly either way. Textbook answers hold up until the interviewer asks what you did when it broke, and that is usually the question that separates candidates. A small real project you can discuss honestly beats a longer list of familiarity claims.

Is dynamic programming still worth learning in 2026?

For interview purposes the question is really whether the teams you are targeting use it, which is worth checking against their actual job postings rather than general popularity rankings. Where it is in use it tends to be deeply embedded and slow to replace.

Should I memorise dynamic programming syntax for the interview?

Rarely worth it. Most interviewers care that you know what to reach for and why, and will not fail you for forgetting an exact flag. Being confidently wrong about behaviour costs far more than admitting you would check the documentation.

What is the most common mistake in dynamic programming interviews?

Answering the question that was asked and stopping there. The strongest candidates add the trade-off or the failure mode without being prompted, which is what signals real use rather than revision.