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.
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!