Dynamic Programming and Recursion Interview Questions

How to approach dynamic programming questions

Dynamic programming (DP) questions usually ask you to count the ways to do something or to find the best way to do it: the fewest coins, the most value, the longest sequence. They're built on recursion, so start by writing a recursive solution that breaks the problem into smaller subproblems of the same shape.

If that recursion solves the same subproblems over and over, you can speed it up: cache each result the first time you compute it (memoization, or top-down), or fill in a table from the smallest subproblems up (bottom-up). Either way, the running time drops from exponential to roughly the number of distinct subproblems times the work for each one.

In the interview, say what a subproblem is out loud (for example, "the number of ways to make amount a using only the first i coins"), then give the base cases and how each answer builds on smaller ones before you write any code. Bottom-up solutions often only need the last row or two of the table, which can cut the space too.

Interview Cake isn't just another question database. Each question below walks you through the problem step by step, with hints and explanations as you need them, just like a real interviewer.

Concepts to review

  • Dynamic programming: What DP is, memoization vs. tabulation, and a step-by-step way to approach a DP problem.
  • Recursion: Base cases, recursive cases, and how each call uses the call stack.
  • Overlapping subproblems: The sign that dynamic programming will help: the same subproblems come up again and again.
  • Memoization: Top-down DP: cache the results of a recursive function.
  • Bottom-up algorithms: Build the answer up from the smallest subproblems, usually with a loop and a table.
  • Base cases: Where a recursive function stops calling itself, and the first thing to get right.
  • The call stack: Why deep recursion uses extra space, and how it can overflow.
  • Greedy algorithms: When the locally best choice is always safe, a greedy algorithm is simpler than DP.

Quick reference: Big O cheat sheet

Dynamic programming and recursion interview FAQ

How do I know if a problem needs dynamic programming?

Look for a problem that asks for a count, a minimum or a maximum over many possible choices, where a brute-force recursive solution would solve the same subproblems again and again. If the best answer to the whole problem can be built from the best answers to smaller versions of it, dynamic programming usually applies.

What's the difference between memoization and bottom-up dynamic programming?

Memoization is top-down: you write the natural recursive solution and cache each result so it's only computed once. Bottom-up starts from the smallest subproblems and builds up to the answer with a loop, which avoids deep recursion. Both usually have the same time complexity.

What's the difference between recursion and dynamic programming?

Recursion is a technique: a function that calls itself on smaller inputs. Dynamic programming is a way to speed up recursive problems whose subproblems overlap, by storing each subproblem's answer instead of computing it again.

What are the most common dynamic programming interview questions?

Classic ones include computing Fibonacci numbers, counting the ways to make change with a set of coins, the knapsack problem, climbing stairs, the longest common subsequence, edit distance, and the longest increasing subsequence.

What do I do if my recursive solution overflows the stack?

Rewrite it iteratively. Bottom-up dynamic programming does this naturally, and for other recursive algorithms you can keep your own stack in a loop. Some languages optimize tail calls so they don't grow the stack, but Python and Java don't.
. . .