A problem has overlapping subproblems if finding its solution involves solving the same subproblem multiple times.
As an example, let's look at the Fibonacci sequence (the series where each number is the sum of the two previous ones—0, 1, 1, 2, 3, 5, 8, ...).
If we wanted to compute the nth Fibonacci number, we could use this simple recursive algorithm:
We'd call fib(n-1) and fib(n-2) subproblems of fib(n).
Now let's look at what happens when we call fib(5):
Our method ends up recursively calling fib(2) three times. So the problem of finding the nth Fibonacci number has overlapping subproblems.
Overlapping Subproblems/Dynamic Programming interview questions to practice
Each one walks you through a real coding interview problem one hint at a time.
- Compute the nth Fibonacci Number » Computer the nth Fibonacci number. Careful--the recursion can quickly spin out of control!
- 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.
Your first 3 questions are free. Just sign in, no credit card. More Overlapping Subproblems/Dynamic Programming questions
See also:
Interview coming up?
Get the free 7-day email crash course. You'll learn how to think algorithmically, so you can break down tricky coding interview questions.
No prior computer science training necessary—we'll get you up to speed quickly, skipping all the overly academic stuff.
No spam. One-click unsubscribe whenever.
You're in! Head over to your email inbox right now to read day one!
Interview Cake