Dynamic Programming for Interviews
What is Dynamic Programming?
Dynamic Programming is an algorithmic technique for optimization problem where we break down our problem in such a way that the optimal solution for our main problem can be derived from optimal solution of our sub-problem.
Let's take an example
Fibonacci numbers are series of numbers where a number in the series is the sum of previous two numbers. The first few elements of the series is 0, 1, 1, 2, 3, 5, 8 ....
We can calculate the fibonacci number as follows:
fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) where n>0
Let us now see how the fibonacci number problem is a dynamic programming problem.
Characteristics of Dynamic Programming
1. Overlapping Subproblems
Subproblem is smaller version of the same problem.
If finding solution to a problem involves calculating the same subproblem multiple times, it is called overlapping subproblem.
In the above diagram we can see, that to calculate fib(4) we need to calculate fib(2) twice, fib(1) thrice and fib(0) twice. So we can clearly see the overlapping subproblem pattern here.
2. Optimal Substructure Property
If the optimal solution for a problem can be constructed using the optimal solutions for the subproblems, it is said to have optimal substructure property.
Here's the fibonacci equation:
fib(n) = fib(n-1) + fib(n-2)
Here we can clearly see that the solution for n can be calculated using solution for n-1 and n-2. So clearly this problem has optimal substructure property as well.
So we can see that Fibonacci number problem can be solved using DP.
Let us now see what DP approach looks like.
DP offers two methods to solve a problem.
1. Top-down with Memoization
In this approach, we still recursively solve the problem but we also have a cache to store the solution to our subproblems so that we don't end up calculating it multiple times.
Whenever we are solving for a sub-problem, we first check the cache if it has already been solved. If solution for sub-problem is already present in cache, return value from the cache else solve for the sub-problem and store the solution in cache for future reference.
2. Bottom-up with Tabulation
Tabulation is the opposite of memoization and avoids recursion.
In this approach, we solve for all the sub-problems first and keep storing the solutions in n-dimensional table. Then based on the values in table, the final solution is calculated and returned.
Solving DP for interviews.
There are mainly 5 patterns in Dynamic programming questions. Here's what we have discussed so far on my site here.
Pattern 1: Using 1D array
NOTE: This section will be updated when I discuss more DP questions.
Follow me for more content to crack your coding interviews. I will be covering all 5 patterns of Dynamic programming questions in Aug so stay tuned !