Dynamic Programming Practice Interview Questions InterviewBit
Dynamic Programming Practice Interview Questions InterviewBit
Participate in Hackathon
(https://www.scaler.com/event/hackx/?
utm_source=ib&utm_medium=top_nudge&utm_campaign=HackX&utm_content=/courses/programming/topics/d
programming//)
70k Coders to compete with!
Courses (/courses/)
/ Programming (/courses/programming/)
/ Dynamic Programming
Be the first one to know about full time and internship opportunities on InterviewBit. ×
Enable Notifications
Level 7
Go To Problems
Dynamic Programming
(https://bit.ly/3oUep0A)
Important tutorials
Read more
Serious about Learning Dynamic
Programming ?
Learn from experts through interactive live sessions.
Try Academy for free (https://www.scaler.com/?utm_source=ib&utm_campaign=free_academy&utm_m
(/search/?q=Google)
Stairs
(/problems/stairs/) 225 15:14
Morgan Stanley (/search/?
(/search/?q=Amazon) Intel
(/search/?q=Intel)
Goldman Sachs (/search/?
q=Goldman Sachs)
Problem Score Companies Time Status
(/search/?q=Amazon)
(/search/?q=Microsoft)
Greedy or dp
Dp tricky
Matrix dp
Rod Cutting
(/problems/rod- 350 69:51
(/search/?q=Google)
cutting/)
Queen Attack
(/problems/queen- 350 61:24
attack/)
Suffix / prefix dp
q=LinkedIn) (/search/?
q=Microsoft)
Arrange II
(/problems/arrange- 350 69:27
Amazon (/search/?
ii/)
q=Amazon)
Derived dp
Chain of Pairs
(/problems/chain-of- 200 38:49
Amazon (/search/?
pairs/)
q=Amazon) Directi
(/search/?q=Directi)
Merge elements
(/problems/merge- 300 54:09
DE Shaw (/search/?
elements/)
q=DE Shaw)
Knapsack
0-1 Knapsack
(/problems/0-1- 200 45:40
Amazon (/search/?
knapsack/)
q=Amazon) deshaw
(/search/?q=deshaw)
Adhoc
Dp optimized backtrack
Multiply dp
q=Twitter) Samsung
(/search/?q=Samsung)
Breaking words
Word Break
(/problems/word- 400 65:45
IBM (/search/?q=IBM)
break/)
(/search/?q=Google)
We can apply DP technique to those problems that exhibit the below 2 characteristics:
1. Optimal Substructures
Any problem is said to be having optimal substructure property if its overall optimal
solution can be evaluated from the optimal solutions of its subproblems.
Consider the example of Fibonacci Numbers.
th
We know that a nth Fibonacci number (Fib(n)) is nothing but sum of previous
2 fibonacci numbers, i.e: Fib(n) = Fib(n-1) + Fib(n-2) .
From the above equation, we can clearly deduce that a problem of size ‘n’
has been reduced to subproblems of size ‘n-1’ and ‘n-2’.
Hence, we can say that Fibonacci numbers have the optimal substructure
property.
2. Overlapping Subproblems
Subproblems are basically the smaller versions of an original problem. Any
problem is said to have overlapping subproblems if calculating its solution involves
solving the same subproblem multiple times.
Let us take the example of finding nth Fibonacci number. Consider evaluating
Fib(5). As shown in the breakdown of steps shown in the image below, we can see
that Fib(5) is calculated by taking sum of Fib(4) and Fib(3) and Fib(4) is calculated
by taking sum of Fib(3) and Fib(2) and so on. Clearly, we can see that the Fib(3),
Fib(2), Fib(1) and Fib(0) has been repeatedly evaluated. These are nothing but the
overlapping subproblems.
Note: It is important for a problem to have BOTH the above specified characteris
tics in order to be eligible to be solved using DP technique.
Dynamic Programming Methods
We shall continue with the example of finding the nth Fibonacci number in order to
understand the DP methods available. We have the following two methods in DP
technique. We can use any one of these techniques to solve a problem in optimised
manner.
2. Write a recursive code for the approach you just thought of.
/**
*/
int Fib(int n) {
3. The time complexity of the above approach based on careful analysis on the
property of recursion shows that it is essentially exponential in terms of
n because some terms are evaluated again and again.
With Memoization
1. Save the results you get for every function run so that if Fib(n) is called again,
you do not recompute the whole thing.
2. Instead of computing again and again, we save the value somewhere. This
process of remembering the values of already run subproblem is called
memoization.
3. Lets declare a global variable memo then.
/**
*/
int Fib(int n) {
if (n <= 1) return n;
return memo[n];
4. Let us now analyze the space and time complexity of this solution. We can try to
improve this further if at all it is possible.
Lets look at the space complexity first.
We use an array of size n for remembering the results of subproblems.
This contributes to a space complexity of O(n) .
Since we are using recursion to solve this, we also end up using stack
memory as part of recursion overhead which is also O(n) . So, overall
space complexity is O(n) + O(n) = 2 O(n) = O(n) .
Lets now look at the time complexity.
Lets look at Fib(n) .
Hence,
= T(n - 2) + 2c
= T(n - 3) + 3c
= T(n - k) + kc
= T(0) + n * c = 1 + n * c = O(n)
#include<stdio.h>
int Fibonacci_Series(int);
int main()
int N, FibN;
scanf("%d", &N);
FibN = Fibonacci_Series(N);
return 0;
int Fibonacci_Series(int N)
if ( N == 0 )
return 0;
else if ( N == 1 )
return 1;
else{
memo[n]=Fibonacci_Series(N - 1) + Fibonacci_Series(N - 2)
return memo[n];
}
Bottom Up Approach (Tabulation)
As the name indicates, bottom up is the opposite of the top-down approach which
avoids recursion.
Here, we solve the problem “bottom-up” way i.e. by solving all the related
subproblems first. This is typically done by populating into an n-dimensional table.
Depending on the results in the table, the solution to the original problem is then
computed. This approach is therefore called as “Tabulation”.
*/
int Fib(int n) {
if (n==0) return 0;
dp[0] = 0;
dp[1] = 1;
return dp[n];
}
#include<stdio.h>
int fib(int n)
int dp[n+1];
int i;
//base cases
dp[0] = 0; dp[1] = 1;
return dp[n];
int main ()
int n = 10;
return 0;
Applications
Before diving into DP, let us first understand where do we use DP.
The core concept of DP is to avoid repeated work by remembering partial results (results
of subproblems). This is very critical in terms of boosting performance and speed of
algorithm. Most of the problems in computer science and real world can be solved using
DP technique.
In real life scenarios, consider the example where I have to go from home to work
everyday. For the first time, I can calculate the shortest path between home and
work by considering all possible routes. But, it is not feasible to do the calculation
every day. Hence, I will be memorizing that shortest path and will be following that
route everyday. In computer science terms, Google Maps will be using DP
algorithm to find the shortest paths between two points.
Apart from the above, DP has found its importance in various fields like
Bioinformatics, Operations research, Decision Making, Image Processing,
MATLAB, MS Word, MS Excel, Financial Optimisations, Genetics, XML indexing
and querying and what not! Read More
FAQs
Why is dynamic programming named “dynamic”?
Step 2. Once you have a approach, write a recursive code for that. Conside
r your recursive code function definition to be as below :
Step 3. Keep track of the results of each function by saving them after ev
ery function call so that if the same function solve(K1, K2, K3 ... ) is c
alled again, we need not compute again.
Step 4. Once we are done, analyze the space and time complexities of the s
olution developed, and try to improve them if possible.
Space The cache entries are All entries starting from the
filled on demand during first one needs to be filled
memoization. by default.
Unlock the complete InterviewBit
(/users/auth/facebook/)
(/users/auth/github/)
(/users/auth/google_oauth2/)
(/users/auth/linkedin/)
Or use email
1 Million +
Questions to Practice
100 +
Blog (https://www.interviewbit.com/blog/)
About Us (/pages/about_us/)
FAQ (/pages/faq/)
Contact Us (/pages/contact_us/)
Terms (/pages/terms/)
Privacy Policy (/pages/privacy/)
Like Us
(https://www.facebook.com/interviewbit)
Follow Us
(https://twitter.com/interview_bit)
Email
(mailto:hello@interviewbit.com)
Unlock the complete InterviewBit
(/users/auth/facebook/)
(/users/auth/github/)
(/users/auth/google_oauth2/)
(/users/auth/linkedin/)
Or use email
1 Million +
Questions to Practice
100 +