Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
144 views

Dynamic Programming Practice Interview Questions InterviewBit

Hackathon for All is a hackathon with Rs 2 Lakh in rewards and career opportunities at MPL and Dunzo. Over 70k coders will compete. Participants can register at the given URL. The document provides information about an upcoming hackathon event.

Uploaded by

Rokibul Hasan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
144 views

Dynamic Programming Practice Interview Questions InterviewBit

Hackathon for All is a hackathon with Rs 2 Lakh in rewards and career opportunities at MPL and Dunzo. Over 70k coders will compete. Participants can register at the given URL. The document provides information about an upcoming hackathon event.

Uploaded by

Rokibul Hasan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Hackathon for All

Win Rewards worth Rs 2 Lakh!


Get career opportunities at MPL and Dunzo

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

What is Dynamic Programming?


Dynamic Programming (commonly referred to as DP) is an algorithmic technique
for solving a problem by recursively breaking it down into simpler subproblems and
using the fact that the optimal solution to the overall problem depends upon the
optimal solution to it’s individual subproblems.
The technique was developed by Richard Bellman in the 1950s.
DP algorithm solves each subproblem just once and then remembers its answer,
thereby avoiding re-computation of the answer for similar subproblem every time.
It is the most powerful design technique for solving optimization related problems.
It also gives us a life lesson - Make life less complex. There is no such thing as
big problem in life. Even if it appears big, it can be solved by breaking into smaller
problems and then solving each optimally.

(https://bit.ly/3oUep0A)
Important tutorials

1. Dynamic View Tutorial


(/tutorial/dynamic-programming-dp-introduction/)
Programming
( Dp )
Introduction :

2. Dynamic View Tutorial


(/tutorial/dynamic-programming-examples/)
Programming
Examples :

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

Dynamic Programming Problems


2d string dp 

Problem Score Companies Time Status

Longest Common 200 41:10


ajio (/search/?q=ajio)
Subsequence
(/problems/longest-
common-subsequence/)

Longest Palindromic 200 39:50


 (/search/?q=LinkedIn)
Subsequence
(/problems/longest-
palindromic-subsequence/)

Edit Distance 300 47:01


 (/search/?q=Google) 
(/problems/edit-distance/)
(/search/?q=LinkedIn) 
(/search/?q=Microsoft)
Amazon (/search/?
q=Amazon)

Repeating Sub-Sequence 300 63:50


 (/search/?q=Google)
(/problems/repeating-
subsequence/)

Distinct Subsequences 325 65:03


 (/search/?q=Google)
(/problems/distinct-
subsequences/)

Scramble String 500 66:44


(/problems/scramble-string/)

Regular Expression Match 500 74:33


 (/search/?q=Facebook)
(/problems/regular-
expression-match/)  (/search/?q=Microsoft)

Regular Expression II 500 65:47


 (/search/?q=Facebook)
(/problems/regular-
expression-ii/)  (/search/?q=Microsoft)

 (/search/?q=Google)

Interleaving Strings 500 60:49


 (/search/?q=Google) 
(/problems/interleaving-
strings/) (/search/?q=Yahoo)
Simple array dp 

Problem Score Companies Time Status

Length of Longest 200 65:01


 (/search/?q=Microsoft)
Subsequence
(/problems/length-of-
longest-subsequence/)

Smallest sequence with 200 65:10


Booking.com (/search/?
given Primes
(/problems/smallest- q=Booking.com)  (/search/?
sequence-with-given- q=Microsoft)
primes/)

Largest area of rectangle 200 74:12


Directi (/search/?q=Directi)
with permutations
(/problems/largest-area-of-
rectangle-with-
permutations/)

Tiling With Dominoes 200 53:00


Directi (/search/?q=Directi)
(/problems/tiling-with-
dominoes/)

Paint House! 200 35:28


 (/search/?q=LinkedIn)
(/problems/paint-house/)

Ways to Decode 225 69:58


 (/search/?q=Facebook)
(/problems/ways-to-
decode/) Amazon (/search/?q=Amazon)

Stairs
(/problems/stairs/) 225 15:14
Morgan Stanley (/search/?

q=Morgan Stanley) Amazon

(/search/?q=Amazon) Intel
(/search/?q=Intel)
Goldman Sachs (/search/?
q=Goldman Sachs)
Problem Score Companies Time Status

Longest Increasing 300 30:39


 (/search/?q=Facebook) 
Subsequence
(/problems/longest- (/search/?q=Yahoo)
increasing-subsequence/) Epic systems (/search/?

q=Epic systems) Amazon

(/search/?q=Amazon) 
(/search/?q=Microsoft)

Intersecting Chords in a 300 67:54


Circle
(/problems/intersecting-
chords-in-a-circle/)

Greedy or dp 

Problem Score Companies Time Status

Tushar's Birthday Bombs 200 76:51


(/problems/tushars-
birthday-bombs/)

Jump Game Array 225 41:15


Amazon (/search/?q=Amazon)
(/problems/jump-game-
array/) Ebay (/search/?q=Ebay)

Min Jumps Array 300 71:11


Amazon (/search/?q=Amazon)
(/problems/min-jumps-
array/) Ebay (/search/?q=Ebay) 
(/search/?q=Google)

Dp tricky 

Problem Score Companies Time Status

Longest Arithmetic 200 73:10


 (/search/?q=Microsoft)
Progression
(/problems/longest-arithmetic-
progression/)
Problem Score Companies Time Status

N digit numbers with digit sum 200 71:37


S (/problems/n-digit-numbers-
with-digit-sum-s-/)

Shortest common superstring 200 61:38


 (/search/?q=Google)
(/problems/shortest-common-
superstring/)

Ways to color a 3xN Board 200 61:32


Codenation (/search/?
(/problems/ways-to-color-a-
q=Codenation)
3xn-board/)

Kth Manhattan Distance 200 59:22


Liv.ai (/search/?q=Liv.ai)
Neighbourhood
(/problems/kth-
manhattan-distance-
neighbourhood/)

Best Time to Buy and Sell 200 55:42


Delhivery (/search/?
Stock atmost B times
(/problems/best-time-to-buy- q=Delhivery) deshaw
and-sell-stock-atmost-b-times/) (/search/?q=deshaw)
Goldman Sachs
(/search/?q=Goldman Sachs)

Coins in a Line 300 62:17


(/problems/coins-in-a-line/)

Evaluate Expression To True 350 67:47


Amazon (/search/?
(/problems/evaluate-
q=Amazon)
expression-to-true/)

Egg Drop Problem! 450 51:43


Liv.ai (/search/?q=Liv.ai)
(/problems/egg-drop-problem/)

Best Time to Buy and Sell 700 62:03


Amazon (/search/?
Stocks III
(/problems/best-
time-to-buy-and-sell-stocks-iii/) q=Amazon)  (/search/?
q=Facebook)

Longest valid Parentheses 700 59:52


 (/search/?q=Google)
(/problems/longest-valid-
parentheses/)
Tree dp 

Problem Score Companies Time Status

Max edge queries! 200 56:09


Juspay Technologies
(/problems/max-edge-queries/)
(/search/?q=Juspay
Technologies)

Max Sum Path in Binary Tree 400 54:39


Directi (/search/?
(/problems/max-sum-path-in-
binary-tree/) q=Directi) Amazon
(/search/?q=Amazon)

Matrix dp 

Problem Score Companies Time Status

Kingdom War 200 57:07


(/problems/kingdom-war/)

Maximum Path in Triangle 200 30:47


Amazon (/search/?
(/problems/maximum-path-in-
q=Amazon)
triangle/)

Maximum Size Square Sub- 200 38:38


Flipkart (/search/?
matrix
(/problems/maximum-size-
q=Flipkart)
square-sub-matrix/)

Increasing Path in Matrix 200 41:40


 (/search/?q=Google)
(/problems/increasing-path-in-
matrix/)

Minimum Difference Subsets! 200 43:41


tower research
(/problems/minimum-difference-
(/search/?q=tower
subsets/)
research) uber
(/search/?q=uber)

Subset Sum Problem! 200 40:48


Zenefits (/search/?
(/problems/subset-sum-problem/)
q=Zenefits)
Problem Score Companies Time Status

Unique Paths in a Grid 300 33:37


 (/search/?
(/problems/unique-paths-in-a-
grid/) q=Facebook)

Dungeon Princess 300 67:50


(/problems/dungeon-princess/)

Min Sum Path in Matrix 300 30:22


Amazon (/search/?
(/problems/min-sum-path-in-
q=Amazon)
matrix/)

Min Sum Path in Triangle 300 42:32


(/problems/min-sum-path-in-
triangle/)

Max Rectangle in Binary Matrix 350 76:48


 (/search/?q=Google)
(/problems/max-rectangle-in-
binary-matrix/)  (/search/?
q=Microsoft)

Rod Cutting
(/problems/rod- 350 69:51
 (/search/?q=Google)
cutting/)

Queen Attack
(/problems/queen- 350 61:24
attack/)

Suffix / prefix dp 

Problem Score Companies Time Status

Sub Matrices with sum Zero 200 71:33


 (/search/?q=Google)
(/problems/sub-matrices-with-
sum-zero/)

Coin Sum Infinite 225 63:49


 (/search/?q=Microsoft)
(/problems/coin-sum-infinite/)
Problem Score Companies Time Status

Max Product Subarray 300 64:20


Amazon (/search/?
(/problems/max-product-
subarray/) q=Amazon)  (/search/?

q=LinkedIn)  (/search/?
q=Microsoft)

Best Time to Buy and Sell 300 28:04


Amazon (/search/?
Stocks I
(/problems/best-time-
to-buy-and-sell-stocks-i/) q=Amazon)  (/search/?
q=Facebook)

Arrange II
(/problems/arrange- 350 69:27
Amazon (/search/?
ii/)
q=Amazon)

Derived dp 

Problem Score Companies Time Status

Chain of Pairs
(/problems/chain-of- 200 38:49
Amazon (/search/?
pairs/)
q=Amazon) Directi
(/search/?q=Directi)

Max Sum Without Adjacent 225 57:41


Epic systems
Elements
(/problems/max-sum-
(/search/?q=Epic
without-adjacent-elements/)
systems)

Merge elements
(/problems/merge- 300 54:09
DE Shaw (/search/?
elements/)
q=DE Shaw)

Knapsack 

Problem Score Companies Time Status

Flip Array (/problems/flip 200 75:04


Flip Array
(/problems/flip- 200 75:04
Problem
array/) Score Companies Time Status

Tushar's Birthday Party 200 68:27


Snapdeal (/search/?
(/problems/tushars-birthday-
q=Snapdeal)
party/)

0-1 Knapsack
(/problems/0-1- 200 45:40
Amazon (/search/?
knapsack/)
q=Amazon) deshaw
(/search/?q=deshaw)

Equal Average Partition 350 72:27


Amazon (/search/?
(/problems/equal-average-
q=Amazon)
partition/)

Adhoc 

Problem Score Companies Time Status

Best Time to Buy and Sell Stocks II 225 40:12


Amazon (/search/?
(/problems/best-time-to-buy-and-
sell-stocks-ii/) q=Amazon) 
(/search/?q=Facebook)

Dp optimized backtrack 

Problem Score Companies Time Status

Word Break II 350 66:06


IBM (/search/?q=IBM) 
(/problems/word-break-ii/)
(/search/?q=Google)

Multiply dp 

Problem Score Companies Time Status


Problem Score Companies Time Status

Unique Binary Search 400 35:33


Amazon (/search/?
Trees II
(/problems/unique-
binary-search-trees-ii/) q=Amazon)  (/search/?

q=Twitter) Samsung
(/search/?q=Samsung)

Count Permutations of BST 400 64:19


(/problems/count-
permutations-of-bst/)

Breaking words 

Problem Score Companies Time Status

Palindrome Partitioning II 400 60:46


Amazon (/search/?
(/problems/palindrome-
partitioning-ii/) q=Amazon)  (/search/?
q=Google)

Word Break
(/problems/word- 400 65:45
IBM (/search/?q=IBM)
break/)
 (/search/?q=Google)

Characteristics of Dynamic Programming


Before moving on to approaches to solve a DP problem, let us have a look at the
characteristics of a problem upon which we can apply the DP technique.

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.

Top Down Approach (Memoization)


Top Down Approach is the method where we solve a bigger problem by recursively
finding the solution to smaller sub-problems. 

Whenever we solve a smaller subproblem, we remember (cache) its result so that


we don’t solve it repeatedly if it’s called many times. Instead of solving repeatedly,
we can just return the cached result.

This method of remembering the solutions of already solved subproblems is


called Memoization.
Pseudo Code and Analysis
Without Memoization

1. Think of a recursive approach to solving the problem. This part is simple. We


already know  Fib(n) = Fib(n - 1) + Fib(n - 2) .

2. Write a recursive code for the approach you just thought of.
/**

* Pseudo code for finding Fib(n) without memoization

* @Parameters: n : nth Fibonacci term needed

*/

int Fib(int n) {

if (n <= 1) return n; //Fib(0)=0; Fib(1)=1

return Fib(n - 1) + Fib(n - 2);

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.
/**

* Pseudo code for finding Fib(n) with memoization

* @Parameters: n : nth Fibonacci term needed

*/

int memo[100] = {0};

int Fib(int n) {

if (n <= 1) return n;

// If we have processed this function before,

// return the result from the last time.

if (memo[n] != 0) return memo[n];

// Otherwise calculate the result and remember it.

memo[n] = Fib(n - 1) + Fib(n - 2);

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

When  Fib(n - 1)  is called, it makes a call to  Fib(n - 2) . So when


the call comes back to the original call from  Fib(n) ,  Fib(n-2)  would
already be calculated. Hence the call to  Fib(n - 2)  will be O(1).

Hence,

T(n) = T(n - 1) + c where c is a constant.

= T(n - 2) + 2c

= T(n - 3) + 3c

= T(n - k) + kc

= T(0) + n * c = 1 + n * c = O(n)

Thanks to Dynamic Programming, we have successfully reduced a


exponential problem to a linear problem.
Implementation

C C++ Java Python

/* C Program to find Nth Fibonacci Number using Memoization */

#include<stdio.h>

int Fibonacci_Series(int);

int memo[100] = {0};

int main()

int N, FibN;

printf("\n Enter the Number to find Nth Fibonacci Number : ");

scanf("%d", &N);

FibN = Fibonacci_Series(N);

printf("\n Fibonacci Number = %d", FibN);

return 0;

int Fibonacci_Series(int N)

if ( N == 0 )

return 0;
else if ( N == 1 )

return 1;

if (memo[n] != 0) return memo[n];

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

Pseudo Code and Analysis


1. We already know  Fib(n) = Fib(n - 1) + Fib(n - 2) .
2. Based on the above relation, we calculate the results of smaller subproblems first
and then build the table.
/**

* Pseudo code for finding Fib(n) using tabulation

* @Parameters: n : nth Fibonacci term needed

* local variable dp[] table built to store results of smaller subproblems

*/

int Fib(int n) {

if (n==0) return 0;

int dp[] = new int[n+1];

//define base cases

dp[0] = 0;

dp[1] = 1;

//Iteratively compute the results and store

for(int i=2; i<=n; i++)

dp[i] = dp[i-1] + dp[i-2];

//return the value corresponding to nth term

return dp[n];
}

3. Analyze the space and time requirements


Lets look at the space complexity first.
In this case too, we use an array of size n for remembering the results
which contributes to a space complexity of  O(n) .
We can further reduce the space complexity from  O(N)  to  O(1)  by
just using 2 variables. This is left as an assignment to the reader.
Lets now look at the time complexity.
Lets look at  Fib(n) .
Here, we solve each subproblem only once in iterative manner. So the
time complexity of the algorithm is also O(N).
Again thanks to DP, we arrived at solution in linear time complexity.
Implementation

C C++ Java Python


/* C Program to find Nth Fibonacci Number using Tabulation */

#include<stdio.h>

int fib(int n)

int dp[n+1];

int i;

//base cases

dp[0] = 0; dp[1] = 1;

//calculating and storing the values

for (i = 2; i <= n; i++)

dp[i] = dp[i-1] + dp[i-2];

return dp[n];

int main ()

int n = 10;

printf("Fibonacci number : %d ", fib(n));

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.

Largest Common Subsequence (LCS) problem - Basis of data comparison


problems and to identify plagiarism in the contents.

Longest Increasing Subsequence problem - used in DNA Matching between two


individuals. Generally, the DNAs are represented as strings and to form a match
between DNAs of two individuals, the algorithm needs to find out the longest
increasing sub sequence between them. In cases of DNA match, the longest
common sub-string (LCS) is also found.
Knapsack Problem You have a bag of limited capacity and you decide to go on a
challenging trek. Due to the capacity restriction, you can only carry certain items in
optimum quantity. How do you select the materials and its quantity in efficient
manner so that you don’t miss out on important items? That’s where DP comes into
aid. 

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”?

According to Richard Bellman’s autobiography “Eye of the Hurricane: An


Autobiography (1984)”, the word “dynamic” was chosen by him to mainly
capture the time-varying aspect of the problems.
How to recognize a problem that can be solved using Dynamic
Programming?

DP is mainly an optimization technique. It is a method for solving problems


by breaking them down into simpler subproblems, solving and storing results
of each subproblem just once. If the same subproblem occurs again, we look
up for the previously stored solution.
Hence, to recognize a problem whether it can be solved using DP, ask
yourself whether the given problem solution can be expressed as a function
of solutions to similar smaller subproblems.
How to solve dynamic programming problems?

The concept of dynamic programming is very simple. If we have solved a


problem with the given input, then we save the result for future reference, so
as to avoid recomputing again.

We follow the mantra - Remember your Past.

If a given problem can be broken up in to smaller subproblems and


these smaller subproblems can be in turn broken down in to even more
smaller ones, and in this process, if we observe some subproblems
which are already solved, then this is a big hint for us to use DP.
In case we are not storing the results, then we are bound to perform
computations unnecessarily which goes against the principle of
dynamic programming. 

 Image Source: Google


We need to know that the optimal solutions to each subproblem contribute to
the optimal solution of the overall given problem.

We can follow the below steps as a guideline for coming up with a DP


solution:
Step 1. Think of a recursive approach to solving the problem which essenti
ally expresses a problem, say P(X), in terms of smaller subproblem, say P
(Y) or an expression involving multiple smaller subproblems, say P(Yi). He
re we expect Yi < X which could mean one of the following:

a. If X is an integer, then it could mean Yi "less than" X arithmetica


lly.

b. If X is a string, it could mean that Yi is a substring of X.

c. If X is an array, it could mean Yi is a subarray of X, and so fort


h.

Step 2. Once you have a approach, write a recursive code for that. Conside
r your recursive code function definition to be as below :

solve(K1, K2, K3 ... )

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.

And that’s how a DP problem is solved.

How is top down approach (memoization) different than bottom up approach


(tabulation)?

Parameters Memoization Tabulation

State State definition can be Complicated to identify


definition thought of easily. what a state should
represent

Ease of Less complicated and Complications increase


code easy to code. when lots of other
conditions arise.

Speed of Slower due to recursive Faster as state values are


execution calls and return accessed directly from
statements table.

Space The cache entries are All entries starting from the
filled on demand during first one needs to be filled
memoization. by default.

Where is dynamic programming used?

Dynamic programming is used in the cases where we solve problems by


dividing them into similar suproblems and then solving and storing their
results so that results are re-used later.
Used in the cases where optimization is needed.
Please refer to Application section above.
What are the characteristics of dynamic programming?

Every DP problem should have optimal substructure and overlapping


subproblems. 

Please refer to Characteristics of Dynamic Programming section above.

What are the applications of dynamic programming?

DP is almost used everywhere which requires guaranteed optimal solution. It


is heavily used in routing, graph problems, computer vision, computer
networks, AI, machine learning etc.
Please refer to Application section above.
How is dynamic programming different from greedy approach?

Parameters Dynamic Programming Greedy Approach

Optimality There is guaranteed optimal Provides no


solution as DP considers all guarantee of getting
possible cases and then optimum approach.
choose the best among them.

Memory DP requires a table or cache More memory


for remembering and this efficient as it never
increases it’s memory looks back or
complexity. revises its previous
choices.
Parameters Dynamic Programming Greedy Approach

Time DP is generally slower due to Generally faster.


complexity considering all possible cases
and then choosing the best
among them.

Feasibility Decision at each step is made Choice is made


after evaluating current which seems best at
problem and solution to the moment in the
previously solved subproblem hope of getting
to calculate optimal solution. global optimal
solution.

How is dynamic programming different from divide and conquer approach?

Divide and Conquer algorithm works by dividing a problem into subproblems,


conquer by solving each subproblem recursively and then combine these
solutions to get solution of the main problem.
Whereas DP is a optimization technique for solving problems in an
optimised manner by dividing problem into smaller subproblems and
then evaluating and storing their results and constructing an optimal
solution for main problem from computed information.
The most important difference in Divide and Conquer strategy is that the
subproblems are independent of each other. When a problem is divided into
subproblems, they do not overlap which is why each subproblem is to be
solved only once.
Whereas in DP, a subproblem solved as part of a bigger problem may
be required to be solved again as part of another subproblem (concept
of overlapping subproblem), so the results of a subproblem is solved
and stored so that the next time it is encountered, the result is simply
fetched and returned.


Unlock the complete InterviewBit

experience for free


Sign Up Using


(/users/auth/facebook/)


(/users/auth/github/)


(/users/auth/google_oauth2/)

(/users/auth/linkedin/)

Or use email
1 Million +

Strong Tech Community


500 +

Questions to Practice
100 +

Expert Interview Guides

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/)

Online C Compiler (/online-c-compiler/)


Online C++ Compiler (/online-cpp-compiler/)

Online Java Compiler (/online-java-compiler/)

Online Javascript Compiler (/online-javascript-compiler/)

Online Python Compiler (/online-python-compiler/)

Scaler Academy Review (/scaler-academy-review)

System Design Interview Questions (/courses/system-design/)

Google Interview Questions (/google-interview-questions/)

Facebook Interview Questions (/facebook-interview-questions/)

Amazon Interview Questions (/amazon-interview-questions/)

Microsoft Interview Questions (/microsoft-interview-questions/)

Javascript Interview Questions (/javascript-interview-questions/)

MVC Interview Questions (/mvc-interview-questions/)

React Interview Questions (/react-interview-questions/)

jQuery Interview Questions (/jquery-interview-questions/)

Angular Interview Questions (/angular-interview-questions/)


Data Structure Interview Questions (/data-structure-interview-questions/)

Selenium Interview Questions (/selenium-interview-questions/)

Directi Interview Questions (/directi-interview-questions/)

Yahoo Interview Questions (/yahoo-interview-questions/)

LinkedIn Interview Questions (/linkedin-interview-questions/)

eBay Interview Questions (/ebay-interview-questions/)

Flipkart Interview Questions (/flipkart-interview-questions/)

Oracle Interview Questions (/oracle-interview-questions/)

Adobe Interview Questions (/adobe-interview-questions/)

C Interview Questions (/c-interview-questions/)

Php Interview Questions (/php-interview-questions/)

C Sharp Interview Questions (/c-sharp-interview-questions/)

Web Api Interview Questions (/web-api-interview-questions/)

Hibernate Interview Questions (/hibernate-interview-questions/)

Node Js Interview Questions (/node-js-interview-questions/)

Cpp Interview Questions (/cpp-interview-questions/)

Oops Interview Questions (/oops-interview-questions/)

Devops Interview Questions (/devops-interview-questions/)

Machine Learning Interview Questions (/machine-learning-interview-questions/)

Docker Interview Questions (/docker-interview-questions/)

Mysql Interview Questions (/mysql-interview-questions/)

Css Interview Questions (/css-interview-questions/)

Laravel Interview Questions (/laravel-interview-questions/)


Asp Net Interview Questions (/asp-net-interview-questions/)

Django Interview Questions (/django-interview-questions/)

Dot Net Interview Questions (/dot-net-interview-questions/)

Kubernetes Interview Questions (/kubernetes-interview-questions/)

Operating System Interview Questions (/operating-system-interview-questions/)

React Native Interview Questions (/react-native-interview-questions/)

Aws Interview Questions (/aws-interview-questions/)

Git Interview Questions (/git-interview-questions/)

Java 8 Interview Questions (/java-8-interview-questions/)

Mongodb Interview Questions (/mongodb-interview-questions/)

Dbms Interview Questions (/dbms-interview-questions/)

Spring Boot Interview Questions (/spring-boot-interview-questions/)

Power Bi Interview Questions (/power-bi-interview-questions/)

Pl Sql Interview Questions (/pl-sql-interview-questions/)

Tableau Interview Questions (/tableau-interview-questions/)

Linux Interview Questions (/linux-interview-questions/)

Ansible Interview Questions (/ansible-interview-questions/)

Java Interview Questions (/java-interview-questions/)

Jenkins Interview Questions (/jenkins-interview-questions/)

Agile Interview Questions (/agile-interview-questions/)

Networking Interview Questions (/networking-interview-questions/)

Azure Interview Questions (/azure-interview-questions/)

Software Testing Interview Questions (/software-testing-interview-questions/)


Automation Testing Interview Questions (/automation-testing-interview-questions/)

Android Interview Questions (/android-interview-questions/)

Hr Interview Questions (/hr-interview-questions/)

Rest Api Interview Questions (/rest-api-interview-questions/)

Unix Interview Questions (/unix-interview-questions/)

Multithreading Interview Questions (/multithreading-interview-questions/)

Typescript Interview Questions (/typescript-interview-questions/)

Angular 8 Interview Questions (/angular-8-interview-questions/)

Salesforce Interview Questions (/salesforce-interview-questions/)

Spark Interview Questions (/spark-interview-questions/)

Bootstrap Interview Questions (/bootstrap-interview-questions/)

Web Services Interview Questions (/web-services-interview-questions/)

Jsp Interview Questions (/jsp-interview-questions/)

Html Interview Questions (/html-interview-questions/)

Hadoop Interview Questions (/hadoop-interview-questions/)

Angularjs Interview Questions (/angularjs-interview-questions/)

Cucumber Interview Questions (/cucumber-interview-questions/)

Cyber Security Interview Questions (/cyber-security-interview-questions/)

Jdbc Interview Questions (/jdbc-interview-questions/)

Microservices Interview Questions (/microservices-interview-questions/)

Ccna Interview Questions (/ccna-interview-questions/)

Data Science Interview Questions (/data-science-interview-questions/)

Uipath Interview Questions (/uipath-interview-questions/)


Cloud Computing Interview Questions (/cloud-computing-interview-questions/)

Spring Interview Questions (/spring-interview-questions/)

Scrum Master Interview Questions (/scrum-master-interview-questions/)

Big Data Interview Questions (/big-data-interview-questions/)

Wcf Interview Questions (/wcf-interview-questions/)

Python Interview Questions (/python-interview-questions/)

Data Analyst Interview Questions (/data-analyst-interview-questions/)

Codeigniter Interview Questions (/codeigniter-interview-questions/)

Maven Interview Questions (/maven-interview-questions/)

Linked List Interview Questions (/linked-list-interview-questions/)

Sql Interview Questions (/sql-interview-questions/)

Servlet Interview Questions (/servlet-interview-questions/)

Flutter Interview Questions (/flutter-interview-questions/)

Shell Scripting Interview Questions (/shell-scripting-interview-questions/)

Ssis Interview Questions (/ssis-interview-questions/)

Entity Framework Interview Questions (/entity-framework-interview-questions/)

Java Collections Interview Questions (/java-collections-interview-questions/)

Java Interview Questions For 5 Years Experience (/java-interview-questions-for-5-years-experience/)

Servicenow Interview Questions (/servicenow-interview-questions/)

Zoho Interview Questions (/zoho-interview-questions/)

Kafka Interview Questions (/kafka-interview-questions/)

Vmware Interview Questions (/vmware-interview-questions/)

Tcs Interview Questions (/tcs-interview-questions/)


Mainframe Interview Questions (/mainframe-interview-questions/)

Infosys Interview Questions (/infosys-interview-questions/)

Informatica Interview Questions (/informatica-interview-questions/)

Pega Interview Questions (/pega-interview-questions/)

Cognizant Interview Questions (/cognizant-interview-questions/)

Es6 Interview Questions (/es6-interview-questions/)

Accenture Interview Questions (/accenture-interview-questions/)


Like Us
(https://www.facebook.com/interviewbit)

Follow Us
(https://twitter.com/interview_bit)

Email
(mailto:hello@interviewbit.com)


Unlock the complete InterviewBit

experience for free


Sign Up Using


(/users/auth/facebook/)


(/users/auth/github/)


(/users/auth/google_oauth2/)


(/users/auth/linkedin/)

Or use email
1 Million +

Strong Tech Community


500 +

Questions to Practice
100 +

Expert Interview Guides

You might also like