|
1 |
| -# dynamic-programming-interview-questions |
| 1 | +<div data-v-5e9078c0="" data-v-b06dc010="" class="QuestionsList"><div data-v-5e9078c0=""><h1 data-v-5e9078c0=""> |
| 2 | + Top 12 Dynamic Programming interview |
| 3 | + questions and answers in 2021. |
| 4 | + |
| 5 | + You can check all |
| 6 | + 12 |
| 7 | + Dynamic Programming interview questions here 👉 |
| 8 | + https://devinterview.io/data/dynamicProgramming-interview-questions |
| 9 | + 🔹 1. What is Dynamic Programming? Answer: Dynamic programming is all about ordering your computations in a way that avoids recalculating duplicate work. More specifically, Dynamic Programming is a technique used to avoid computing multiple times the same subproblem in a recursive algorithm. DP algorithms could be implemented with recursion, but they don't have to be. With dynamic programming, you store your results in some sort of table generally. When you need the answer to a problem, you reference the table and see if you already know what it is. If not, you use the data in your table to give yourself a stepping stone towards the answer. There are two approaches to apply Dynamic Programming: - The top-down or memoization. When the recursion does a lot of unecessary calculation, an easy way to solve this is to cache the results and to check before executing the call if the result is already in the cache.
- The bottom-up or tabulation approach. A better way to do this is to get rid of the recursion all-together by evaluating the results in the right order and building the array as we iterate. The partial results are available when needed if the iteration is done in the right order.
TOP of the tree
|
| 10 | +<span class="token cMod">fibspan><span class="token cBase">(span><span class="token cNum">4span><span class="token cBase">)span> |
| 11 | + <span class="token cMod">fibspan><span class="token cBase">(span><span class="token cNum">3span><span class="token cBase">)span><span class="token cBase">...span><span class="token cBase">...span><span class="token cBase">...span><span class="token cBase">...span><span class="token cBase">...span><span class="token cBase">...span><span class="token cBase">...span><span class="token cBase">.span> <span class="token cBase">+span> <span class="token cMod">fibspan><span class="token cBase">(span><span class="token cNum">2span><span class="token cBase">)span> |
| 12 | + <span class="token cMod">fibspan><span class="token cBase">(span><span class="token cNum">2span><span class="token cBase">)span><span class="token cBase">...span><span class="token cBase">...span><span class="token cBase">...span> <span class="token cBase">+span> <span class="token cMod">fibspan><span class="token cBase">(span><span class="token cNum">1span><span class="token cBase">)span> <span class="token cMod">fibspan><span class="token cBase">(span><span class="token cNum">1span><span class="token cBase">)span><span class="token cBase">...span><span class="token cBase">...span><span class="token cBase">...span><span class="token cBase">.span><span class="token cBase">.span> <span class="token cBase">+span> <span class="token cMod">fibspan><span class="token cBase">(span><span class="token cNum">0span><span class="token cBase">)span> |
| 13 | + <span class="token cMod">fibspan><span class="token cBase">(span><span class="token cNum">1span><span class="token cBase">)span> <span class="token cBase">+span> <span class="token cMod">fibspan><span class="token cBase">(span><span class="token cNum">0span><span class="token cBase">)span> <span class="token cMod">fibspan><span class="token cBase">(span><span class="token cNum">1span><span class="token cBase">)span> <span class="token cMod">fibspan><span class="token cBase">(span><span class="token cNum">1span><span class="token cBase">)span> <span class="token cMod">fibspan><span class="token cBase">(span><span class="token cNum">0span><span class="token cBase">)span> |
| 14 | + <span class="token cMod">fibspan><span class="token cBase">(span><span class="token cNum">1span><span class="token cBase">)span> <span class="token cMod">fibspan><span class="token cBase">(span><span class="token cNum">0span><span class="token cBase">)span> |
| 15 | +BOTTOM of the tree
🔹 2. How Dynamic Programming is different from Recursion and Memoization? Answer: - Memoization is when you store previous results of a function call (a real function always returns the same thing, given the same inputs). It doesn't make a difference for algorithmic complexity before the results are stored.
- Recursion is the method of a function calling itself, usually with a smaller dataset. Since most recursive functions can be converted to similar iterative functions, this doesn't make a difference for algorithmic complexity either.
- Dynamic programming is the process of solving easier-to-solve sub-problems and building up the answer from that. Most DP algorithms will be in the running times between a Greedy algorithm (if one exists) and an exponential (enumerate all possibilities and find the best one) algorithm.
- DP algorithms could be implemented with recursion, but they don't have to be.
- DP algorithms can't be sped up by memoization, since each sub-problem is only ever solved (or the "solve" function called) once.
🔹 3. What are some characteristics of Dynamic Programming? Answer: The key idea of DP is to save answers of overlapping smaller sub-problems to avoid recomputation. For that: - An instance is solved using the solutions for smaller instances.
- The solutions for a smaller instance might be needed multiple times, so store their results in a table.
- Thus each smaller instance is solved only once.
- Additional space is used to save time.
🔹 4. What are pros and cons of Memoization or Top-Down approach? Answer: Pros: - Memoization is very easy to code (you can generally* write a "memoizer" annotation or wrapper function that automatically does it for you), and should be your first line of approach. It feels more natural. You can take a recursive function and memoize it by a mechanical process (first lookup answer in cache and return it if possible, otherwise compute it recursively and then before returning, you save the calculation in the cache for future use), whereas doing bottom up dynamic programming requires you to encode an order in which solutions are calculated.
- Top-down only solves sub-problems used by your solution whereas bottom-up might waste time on redundant sub-problems.
Cons: - With memoization, if the tree is very deep (e.g.
fib(106) ), you will run out of stack space, because each delayed computation must be put on the stack, and you will have 106 of them.
🔹 5. What should you consider when choosing between Top-Down vs Bottom-Up solutions for the same problem? |
| 16 | + 👉🏼 Check |
| 17 | + <a href="https://devinterview.io/data/dynamicProgramming-interview-questions">all 12 answersa>div> <br><br>div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 6. What are some pros and cons of Tabulation or Bottom-Up approach?h2>div> <div> |
| 18 | + 👉🏼 Check |
| 19 | + <a href="https://devinterview.io/data/dynamicProgramming-interview-questions">all 12 answersa>div> <br><br>div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 7. What is the difference between Divide and Conquer and Dynamic Programming Algorithms?h2>div> <div> |
| 20 | + 👉🏼 Check |
| 21 | + <a href="https://devinterview.io/data/dynamicProgramming-interview-questions">all 12 answersa>div> <br><br>div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 8. Provide an example of Dynamic Program but without Recursionh2>div> <div> |
| 22 | + 👉🏼 Check |
| 23 | + <a href="https://devinterview.io/data/dynamicProgramming-interview-questions">all 12 answersa>div> <br><br>div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 9. LIS: Find length of the longest increasing subsequence (LIS) in the array. Solve using DP.h2>div> <div> |
| 24 | + 👉🏼 Check |
| 25 | + <a href="https://devinterview.io/data/dynamicProgramming-interview-questions">all 12 answersa>div> <br><br>div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 10. Compare Greedy vs Divide & Conquer vs Dynamic Programming Algorithmsh2>div> <div> |
| 26 | + 👉🏼 Check |
| 27 | + <a href="https://devinterview.io/data/dynamicProgramming-interview-questions">all 12 answersa>div> <br><br>div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 11. Is Dijkstra's algorithm a Greedy or Dynamic Programming algorithm?h2>div> <div> |
| 28 | + 👉🏼 Check |
| 29 | + <a href="https://devinterview.io/data/dynamicProgramming-interview-questions">all 12 answersa>div> <br><br>div><div data-v-5e9078c0="" class="unit"><div><h2>🔹 12. How to use Memoization for N-th Fibonacci number?h2>div> <div> |
| 30 | + 👉🏼 Check |
| 31 | + <a href="https://devinterview.io/data/dynamicProgramming-interview-questions">all 12 answersa>div> <br><br>div> <div data-v-5e9078c0="" class="end">div> <br data-v-5e9078c0=""> |
| 32 | + Thanks 🙌 for reading and good luck on your next tech interview! |
| 33 | + <br data-v-5e9078c0=""> |
| 34 | + Explore 3800+ dev interview question here 👉 |
| 35 | + <a data-v-5e9078c0="" href="https://devinterview.io/">Devinterview.ioa>div> div> |
0 commit comments