Dynamic Programming Course Intro
- 63Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is dynamic programming?
63 questions
- 1
What is dynamic programming?
Model answer
Dynamic programming is a method for solving complex problems by breaking them down into simpler subproblems that overlap and storing the results of those subproblems to avoid redundant computation.
Explanation
Dynamic programming leverages overlapping subproblems and optimal substructure to optimize recursive solutions.
- 2
What are the two key characteristics a problem must have to be solvable by dynamic programming?
Model answer
Overlapping subproblems and optimal substructure.
Explanation
Overlapping subproblems means the same subproblem occurs multiple times; optimal substructure means the optimal solution can be built from optimal solutions of subproblems.
- 3
What is memoization?
Model answer
Memoization is a top-down dynamic programming technique where the results of expensive function calls are stored (cached) and reused when the same inputs occur again.
Explanation
Typically implemented using a hash map where keys are function arguments and values are return values.
- 4
What is tabulation?
Model answer
Tabulation is a bottom-up dynamic programming technique where a table (usually an array) is filled iteratively from the base cases upward to the final solution.
Explanation
Tabulation avoids recursion entirely and is often implemented with loops.
- 5
How does memoization differ from tabulation?
Model answer
Memoization is a top-down approach that uses recursion and caches results; tabulation is a bottom-up approach that fills a table iteratively without recursion.
Explanation
Memoization starts with the original problem and recurses down to subproblems; tabulation starts from base cases and builds up.
- 6
When analyzing a recursive brute force solution, what does the height of the recursion tree represent?
Model answer
The height represents the number of recursive calls along the longest path from the root to a base case, which corresponds to the maximum stack depth.
Explanation
For Fibonacci, the height is approximately n because each call reduces n by 1 or 2.
- 7
What does the branching factor of the recursion tree represent?
Model answer
The branching factor is the maximum number of recursive calls made from a single node, reflecting the number of choices or options at each step.
Explanation
For Fibonacci, the branching factor is 2; for canSum, it is the length of the numbers array.
- 8
For a recursive function with a single integer input n and branching factor b, what is the time complexity in terms of Big O?
Model answer
The time complexity is O(bn) if the height is n and each node branches to b children.
Explanation
This is typical for exponential recursive algorithms like naive Fibonacci.
- 9
How is space complexity analyzed for recursive functions?
Model answer
Space complexity includes the maximum depth of the call stack (height of the recursion tree) plus any auxiliary data structures used.
Explanation
For Fibonacci, space is O(n) because the stack depth is n.
- 10
In the Fibonacci example, why is the brute force time complexity O(2n)?
Model answer
Each recursive call makes two further calls, and the recursion depth is n, resulting in about 2n total calls.
Explanation
The recursion tree is binary with height n, so number of nodes is exponential.
- 11
What is the space complexity of the brute force Fibonacci function?
Model answer
O(n) due to the call stack depth.
Explanation
The maximum number of recursive calls on the stack at any time equals the depth of the recursion tree, which is n.
- 12
What is the purpose of a memo object in memoization?
Model answer
To store results of subproblems (function calls) so that if the same subproblem occurs again, the stored result can be returned immediately without recomputation.
Explanation
The memo object uses function arguments as keys and return values as values.
- 13
What should be used as keys in the memo object?
Model answer
The arguments to the function, typically encoded as a string if there are multiple arguments (e.g., concatenated with a separator).
Explanation
For single arguments, the argument itself can be used as the key; for multiple, combine them to form a unique key.
- 14
What should be the values in the memo object?
Model answer
The return value of the function for the given arguments.
Explanation
The value stored is the result that the function would compute for that set of inputs.
- 15
In the memoization recipe, what are the two main steps?
Model answer
First, implement a brute force recursive solution. Second, add memoization by introducing a memo object, checking it before recursive calls, and storing results after computation.
Explanation
This keeps the logic clean: first ensure correctness, then optimize efficiency.
- 16
Why is it important to get a brute force recursive solution first before adding memoization?
Model answer
Because memoization is a formulaic addition to a correct recursive solution; it's easier to ensure correctness first and then optimize.
Explanation
Many students try to solve both at once, leading to errors; separating the steps simplifies debugging.
- 17
How does memoization reduce time complexity in Fibonacci?
Model answer
It reduces the time complexity from O(2n) to O(n) because each Fibonacci number from 1 to n is computed only once.
Explanation
The memoized Fibonacci tree becomes a linear chain with a limited number of duplicate subtrees eliminated.
- 18
What is the first step in the tabulation strategy?
Model answer
Create a table (array) of size appropriate for the input, usually with one extra element to account for zero-indexing.
Explanation
The table size is typically input size plus one so that the index matches the input value.
- 19
How do you determine the size of the table in tabulation?
Model answer
The size is based on the input that changes as subproblems shrink; for a single integer n, the table has length n+1.
Explanation
For grid traveler with dimensions m and n, the table is (m+1) x (n+1).
- 20
What is the typical initial seed value in a tabulation table for counting problems?
Model answer
Index 0 (for empty input) is often seeded with 1 for counting problems, and all other entries are initialized to 0.
Explanation
For Fibonacci, table[0] = 0 and table[1] = 1; for grid traveler, table[1][1] = 1.
- 21
In the Fibonacci tabulation example, why is the table size n+1?
Model answer
Because the index of the array corresponds exactly to the Fibonacci number we want; the last index n should hold the answer.
Explanation
If n=6, we need indices 0 through 6, so length 7.
- 22
How does tabulation for Fibonacci work?
Model answer
Initialize table[0]=0, table[1]=1. For i from 0 to n-1, add table[i] to table[i+1] and table[i+2] (if within bounds). Finally return table[n].
Explanation
Each position contributes to its next two positions, building up to the nth Fibonacci number.
- 23
What is the time and space complexity of tabulated Fibonacci?
Model answer
Time O(n), space O(n).
Explanation
A single loop over n elements and an array of size n+1.
- 24
In the canSum problem, what does the function return?
Model answer
A boolean (true or false) indicating whether the target sum can be generated by adding numbers from the given array (repetition allowed).
Explanation
It is a decision problem: yes or no.
- 25
In the canSum brute force recursion, what are the base cases?
Model answer
If target sum is 0, return true. If target sum is negative, return false.
Explanation
Target 0 is trivially possible (take no numbers); negative is impossible because numbers are non-negative.
- 26
How does the canSum recursion transition from one state to the next?
Model answer
For each number in the array, subtract it from the target sum to get a new remainder, and recursively check if that remainder can be summed.
Explanation
The remainder becomes the new target sum for the recursive call, effectively reducing the problem size.
- 27
What is the time complexity of brute force canSum?
Model answer
O(nm) where m is the target sum and n is the length of the numbers array.
Explanation
The recursion tree has height m and branching factor n, so total nodes about nm.
- 28
How does memoization improve canSum complexity?
Model answer
It reduces time to O(m⋅n) because there are only m distinct target sums, and for each we iterate over n numbers.
Explanation
Memoization stores results for each target sum encountered, avoiding recomputation.
- 29
In the howSum problem, what does the function return?
Model answer
An array containing one combination of numbers that sum to the target, or null if no combination exists.
Explanation
It returns any valid combination (not necessarily the shortest).
- 30
How does the howSum recursion build the combination array?
Model answer
When a recursive call returns a non-null array (combination for the remainder), the current number is appended to that array and the result is returned.
Explanation
This builds the combination by adding numbers along the successful path.
- 31
What additional operation in howSum adds extra time complexity compared to canSum?
Model answer
Copying the array returned from the recursive call, which takes O(m) time in the worst case.
Explanation
Because the array length can be up to m (e.g., all ones), and this copy is done per recursive call.
- 32
In the bestSum problem, what is the difference from howSum?
Model answer
bestSum returns the shortest combination (minimum number of elements) that sums to the target, while howSum returns any valid combination.
Explanation
bestSum compares candidate combinations and keeps the shortest one.
- 33
How does bestSum decide which combination to keep?
Model answer
It iterates through all possible combinations and updates a variable storing the shortest combination found so far.
Explanation
At the end of recursion, it returns the shortest combination (or null if none).
- 34
What is the time complexity of memoized bestSum?
Model answer
O(m2⋅n), where m is target sum and n is length of numbers array.
Explanation
Similar to howSum but with additional comparison; the array copying still takes O(m) per recursive call.
- 35
In the canConstruct problem, what does the function return?
Model answer
A boolean indicating whether the target string can be formed by concatenating words from the word bank (repetition allowed).
Explanation
Similar to canSum but with strings.
- 36
How do you transition from one node to the next in the canConstruct recursion?
Model answer
For each word in the word bank, if it is a prefix of the target string, recursively check if the suffix (the target with the prefix removed) can be constructed.
Explanation
Only prefixes are used; removing from the middle is not allowed because it would create new adjacencies.
- 37
What is the base case for canConstruct?
Model answer
If the target string is empty, return true (empty string is always constructible).
Explanation
You can take no words from the word bank to form an empty string.
- 38
Why must you only consider prefixes, not substrings in the middle?
Model answer
Because removing a substring from the middle would create new adjacent characters that were not originally adjacent, potentially creating false matching possibilities.
Explanation
Only prefixes maintain the integrity of the remaining string.
- 39
What is the time complexity of brute force canConstruct?
Model answer
O(nm⋅m), where m is length of target string and n is number of words in word bank.
Explanation
The recursion tree height is m (worst-case single character removals), branching factor n, and string slicing adds O(m) per call.
- 40
How does memoization improve canConstruct?
Model answer
It reduces time to O(m2⋅n), as there are at most m distinct suffixes and for each we iterate over n words and do O(m) slicing.
Explanation
Memoization stores results for each suffix, eliminating duplicate subtree explorations.
- 41
In countConstruct, what does the function return?
Model answer
A number representing the total number of ways to construct the target string using words from the word bank.
Explanation
It is a counting problem, returning the count of distinct combinations.
- 42
How does countConstruct use the return value from subproblems?
Model answer
It sums the return values from all recursive calls (each representing the number of ways for the suffix), and returns the total sum.
Explanation
Each way to form the suffix combined with the current prefix gives a way to form the larger target.
- 43
In allConstruct, what does the function return?
Model answer
A two-dimensional array containing all possible combinations (as arrays of words) that form the target string.
Explanation
The outer array holds all distinct combinations; each inner array is a sequence of words whose concatenation equals the target.
- 44
Why is allConstruct inherently exponential in complexity?
Model answer
Because the number of distinct combinations to return can be exponential in the length of the target string (e.g., if the word bank contains many single-character words).
Explanation
Even the output size itself can be exponential, so any algorithm must at least be exponential in time and space.
- 45
In the grid traveler problem, what are the allowed moves?
Model answer
Only moves to the right or down; you cannot move left or up.
Explanation
This restricts the path to monotonic moves from top-left to bottom-right.
- 46
What is the base case for grid traveler?
Model answer
If the grid is 1x1, return 1 (one way: do nothing). If either dimension is 0, return 0 (no valid grid).
Explanation
A 1x1 grid is trivially traveled; a dimension of 0 means an invalid or empty grid.
- 47
How does the grid traveler recursion shrink the problem?
Model answer
Moving down reduces the number of rows by 1; moving right reduces the number of columns by 1. The function is called recursively on the remaining subgrid.
Explanation
This transforms the problem into a smaller instance of the same problem.
- 48
What is the time complexity of brute force grid traveler?
Model answer
O(2m+n), where m is number of rows and n is number of columns.
Explanation
The recursion tree height is m+n (since each move reduces one dimension), and branching factor is 2 (right or down).
- 49
How does memoization improve grid traveler?
Model answer
It reduces time to O(m⋅n) because there are at most m*n distinct subproblems (grid sizes) and each is computed once.
Explanation
Memoization stores results for each (m,n) pair, eliminating redundant calculations.
- 50
How does tabulation work for grid traveler?
Model answer
Create a 2D table of size (m+1) x (n+1) initialized with 0. Set table[1][1]=1. For each cell, add its value to the cell below and to the right. Finally return table[m][n].
Explanation
The value at each cell represents the number of ways to travel from that cell to the bottom-right.
- 51
What is the time and space complexity of tabulated grid traveler?
Model answer
Time O(m⋅n), space O(m⋅n).
Explanation
The nested loops iterate over all cells, and the table stores each cell.
- 52
In the memoization recipe, after adding the memo object, what additional base case must be added?
Model answer
A check: if the current arguments are already a key in the memo, return the stored value immediately.
Explanation
This avoids recomputation if the subproblem has been solved before.
- 53
In tabulation for canSum, how is the table initialized?
Model answer
Create an array of length target+1, fill with false, then set table[0] = true.
Explanation
False indicates the sum is not yet reachable; true at index 0 is the seed for the empty sum.
- 54
In tabulation for canSum, how do you fill the table?
Model answer
For each index i from 0 to target, if table[i] is true, then for each number num in the array, set table[i+num] = true (if within bounds).
Explanation
If sum i is reachable, then adding num makes sum i+num reachable.
- 55
In tabulation for howSum, what is stored in the table?
Model answer
Each cell stores an array representing one combination that sums to that index, or null if not reachable. The seed at index 0 is an empty array.
Explanation
The array is a combination of numbers that sum to the index value.
- 56
In tabulation for howSum, how do you propagate combinations?
Model answer
For each index i with a non-null array, for each num, copy that array and append num, then store (or replace) at index i+num.
Explanation
This builds combinations by extending existing ones.
- 57
In tabulation for bestSum, what additional logic is needed?
Model answer
When copying a combination to a future index, compare its length with any existing combination at that index and keep the shorter one.
Explanation
This ensures the table always holds the shortest combination for each sum.
- 58
In tabulation for canConstruct, how is the table structured?
Model answer
A boolean array of length target.length+1, where table[i] indicates whether the prefix of length i (from 0 to i-1) can be constructed. Seed table[0]=true.
Explanation
The indices correspond to the length of the prefix, not positions.
- 59
In tabulation for canConstruct, how do you fill the table?
Model answer
For each index i from 0 to target length, if table[i] is true, then for each word that is a prefix of the suffix starting at i, set table[i+word.length]=true.
Explanation
If the prefix up to i is constructible, then adding the word extends it to a larger prefix.
- 60
In tabulation for countConstruct, what is stored in the table?
Model answer
A number array where table[i] is the number of ways to construct the prefix of length i. Seed table[0]=1.
Explanation
Counting is done by adding the value at the current index to future indices.
- 61
In tabulation for allConstruct, what is stored in the table?
Model answer
A 2D array where table[i] is an array of all combinations (each combination is an array of words) that form the prefix of length i. Seed table[0]=[ [] ].
Explanation
Each combination is a list of words; the table holds all such lists for each prefix.
- 62
In the canSum memoization, what key is used in the memo object?
Model answer
The target sum itself (as a number), since the numbers array does not change across recursive calls.
Explanation
The only varying argument is the target sum, so it uniquely identifies the subproblem.
- 63
In the howSum memoization, what key is used?
Model answer
The target sum (as a number) because the numbers array remains constant.
Explanation
Same reasoning as canSum; the key is the target sum.

Ask anything
We've read this whole study set, so questions about it get the best answers.