Dynamic Programming Flashcards Preview

Software Developer Interview Prep > Dynamic Programming > Flashcards

Flashcards in Dynamic Programming Deck (11)
Loading flashcards...
1

Which class of problems can DP solve efficiently?

Optimization problems. Like minimizing something, maximizing something, best of something etc. Some examples are Shortest Path, Maximum subsequence of two strings, Knapsack etc.

2

Which are two ways to solve DP problems?

Bottom Up or Top Down

3

Define Bottom Up approach to DP?

You compute base case first and store that in table and then next moves are dependent on base case. You compute dp[0] first, dp[1] and then till dp[n]. dp[n] is our answer. So all subsequent steps are dependent on some calculation we have already performed.

4

Define Top Down approach to DP?

Recursion based solution. dp(n) -> dp(n-1) -> ... dp(0).

5

Which is the first step to solve a DP problem
?

First step is to figure out subproblems which can help to solve the original problem. They don't have to be same as original problem at hand, but should somehow help in achieving to the solution. Then memoize the answer to subproblem so that if we have to solve it again we can reuse the answer.

6

Running time of DP problem?

No of subproblems * Time to solve each subproblem. So for fibonacci the running time will be n (subproblems) * O(1) constant time per subproblem (multiplication). Here we ignore the recursive call overhead (for the memoized calls).

7

What kind of sort are we doing for subproblems in DP?

We are doing a "topological sort" of the subproblems in the DAG (Directed Acyclic Graph) of subproblem dependency.

8

How much space do you need for memoization of fibonacci?

If we create a DAG and write code you will realize that you only need to remember last two values. So we can achieve in constant space and linear time.

9

Which is a tool that you have when you don't know the answer but you would like to know?

Guessing. Guess and optimize for the best guess. So we guess but intelligently. Algorithmically we don't just guess one, we guess all guesses. So this is similar to Brute Force but carefully.

10

Which are the central concepts in DP?

DP is equivalent to recursion+ memoization + guessing

11

Which is the one thing needed for memoization to work?

Its that the subproblem dependencies should be acyclic. Otherwise we get an infinite algorithm.