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.
Dynamic programming and recursion practice questions
Write a recursive function of generating all permutations of an input string. keep reading »
Compute the nth Fibonacci Number »Computer the nth Fibonacci number. Careful--the recursion can quickly spin out of control! keep reading »
Making Change »Count the number of ways to make an amount of money from a set of coin denominations (the number-of-ways version of coin change). Step-by-step solution. keep reading »
The Cake Thief »Maximize the value of cakes that fit in a bag when you can take unlimited copies of each: the unbounded knapsack problem, solved step by step. keep reading »
Keep going with the Dynamic programming and recursion section of our coding interview course.
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?
What's the difference between memoization and bottom-up dynamic programming?
What's the difference between recursion and dynamic programming?
What are the most common dynamic programming interview questions?
What do I do if my recursive solution overflows the stack?
More interview questions by topic
- Array and string interview questions
- Hash table interview questions
- Greedy algorithm interview questions
- Sorting and binary search interview questions
- Binary tree and BST interview questions
- Graph interview questions
- Stack and queue interview questions
- Linked list interview questions
- Bit manipulation interview questions
- System design interview questions
- All coding interview questions
Interview Cake