1. Home
  2. Explore
  3. Computer Science
  4. Dynamic Programming Course Intro

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Dynamic Programming Course Intro

  • 41Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Dynamic Programming Fundamentals

What is the primary characteristic that identifies a problem as a dynamic programming problem?

41 questions

  1. 1

    What is the primary characteristic that identifies a problem as a dynamic programming problem?

    1. A

      It involves sorting and searching operations.

    2. B

      It can be solved recursively without any optimization.

    3. C

      It requires the use of exponential time algorithms.

    4. D

      It has overlapping subproblems and optimal substructure.

    Correct answer
    D

    It has overlapping subproblems and optimal substructure.

    Explanation

    Dynamic programming problems are defined by having overlapping subproblems (the same subproblem appears multiple times) and optimal substructure (the optimal solution can be constructed from optimal solutions of subproblems).

  2. 2

    What is the time complexity of the classic recursive Fibonacci function without memoization?

    1. A

      O(2n)O(2^n)O(2n)

    2. B

      O(n2)O(n^2)O(n2)

    3. C

      O(nlog⁡n)O(n \log n)O(nlogn)

    4. D

      O(n)O(n)O(n)

    Correct answer
    A

    O(2n)O(2^n)O(2n)

    Explanation

    The recursive Fibonacci function has exponential time complexity O(2n)O(2^n)O(2n) because each call branches into two recursive calls, leading to a binary tree of calls with height n.

  3. 3

    When implementing memoization for a recursive function, what should be used as the key in the memo object?

    1. A

      The return value of the function.

    2. B

      The depth of the recursion.

    3. C

      The arguments that change across recursive calls.

    4. D

      The line number in the code.

    Correct answer
    C

    The arguments that change across recursive calls.

    Explanation

    The memo key should be a representation of the function arguments that uniquely identifies a subproblem. Typically, we use the arguments that change (e.g., target sum or target string) because the other arguments remain constant.

  4. 4

    In tabulation, how do you determine the size of the table?

    1. A

      Based on the number of recursive calls.

    2. B

      Based on the number of possible outputs.

    3. C

      Based on the length of the numbers array.

    4. D

      Based on the input that changes, plus one for the base case.

    Correct answer
    D

    Based on the input that changes, plus one for the base case.

    Explanation

    In tabulation, the table size is typically based on the input that changes (e.g., target sum or target string length) plus one, so that indices align properly and there is space for the base case (e.g., index 0).

  5. 5

    In the grid traveler problem, what is the time complexity of the optimal memoized solution?

    1. A

      O(m×n)O(m \times n)O(m×n)

    2. B

      O(m+n)O(m + n)O(m+n)

    3. C

      O(max⁡(m,n))O(\max(m, n))O(max(m,n))

    4. D

      O(2m+n)O(2^{m+n})O(2m+n)

    Correct answer
    A

    O(m×n)O(m \times n)O(m×n)

    Explanation

    The memoized grid traveler has time complexity O(m×n)O(m \times n)O(m×n) because the number of unique subproblems is roughly m×nm \times nm×n (all combinations of rows and columns), and each subproblem is solved once.

  6. 6

    What is the base case for the 'can sum' problem when the target sum is zero?

    1. A

      Return false

    2. B

      Return null

    3. C

      Return true

    4. D

      Return an empty array

    Correct answer
    C

    Return true

    Explanation

    If the target sum is zero, it is always possible to generate that sum by taking no numbers from the array, so the function should return true.

  7. 7

    What does the 'how sum' problem return if it is impossible to generate the target sum?

    1. A

      0

    2. B

      false

    3. C

      An empty array

    4. D

      null

    Correct answer
    D

    null

    Explanation

    If no combination of numbers adds up to the target sum, the function should return null, indicating that no combination exists.

  8. 8

    In the 'best sum' problem, if there are multiple ways to generate the target sum, which one should be returned?

    1. A

      The shortest combination

    2. B

      Any combination

    3. C

      The first one found

    4. D

      The longest combination

    Correct answer
    A

    The shortest combination

    Explanation

    The 'best sum' problem asks for the shortest combination (fewest numbers) that adds up to the target sum. If there is a tie, any shortest combination is acceptable.

  9. 9

    In the 'can construct' problem, what does the function return?

    1. A

      An array of all possible constructions

    2. B

      A number indicating the number of ways

    3. C

      The shortest construction

    4. D

      A boolean indicating whether it is possible

    Correct answer
    D

    A boolean indicating whether it is possible

    Explanation

    The 'can construct' function returns a boolean (true or false) indicating whether the target string can be formed by concatenating words from the word bank.

  10. 10

    What is the correct base case for the 'count construct' problem?

    1. A

      If target is empty, return an empty array

    2. B

      If target is empty, return null

    3. C

      If target is empty, return 0

    4. D

      If target is empty, return 1

    Correct answer
    D

    If target is empty, return 1

    Explanation

    If the target string is empty, there is exactly one way to construct it: by using no words. So the base case returns 1.

  11. 11

    In the 'all construct' problem, what should be returned if the target string cannot be constructed?

    1. A

      An empty one-dimensional array

    2. B

      An array containing an empty array

    3. C

      null

    4. D

      An array with a single null element

    Correct answer
    A

    An empty one-dimensional array

    Explanation

    If no construction is possible, the function returns an empty one-dimensional array ([]), indicating that there are zero ways to construct the target.

  12. 12

    When using tabulation for the Fibonacci sequence, what is the initial value in the table at index 0?

    1. A

      1

    2. B

      0

    3. C

      undefined

    4. D

      null

    Correct answer
    B

    0

    Explanation

    In the Fibonacci sequence starting with 0 and 1, the 0th Fibonacci number is 0, so index 0 of the table is seeded with 0.

  13. 13

    In the recursive brute-force solution for 'can sum', what is the time complexity?

    1. A

      O(nm)O(n^m)O(nm)

    2. B

      O(mn)O(m^n)O(mn)

    3. C

      O(n×m)O(n \times m)O(n×m)

    4. D

      O(2n+m)O(2^{n+m})O(2n+m)

    Correct answer
    A

    O(nm)O(n^m)O(nm)

    Explanation

    The brute-force recursive can sum has time complexity O(nm)O(n^m)O(nm), where n is the length of the numbers array and m is the target sum. The branching factor is n, and the height of the recursion tree is m.

  14. 14

    What is a common mistake when implementing memoization for grid traveler?

    1. A

      Initializing the memo with an array instead of an object

    2. B

      Not passing the memo object to recursive calls

    3. C

      Using a string key like 'm,n' but forgetting a separator

    4. D

      Returning the memo value before checking the base case

    Correct answer
    C

    Using a string key like 'm,n' but forgetting a separator

    Explanation

    A common mistake is to concatenate m and n without a separator (e.g., '42' + '3' becomes '423'), which can cause collisions. Using a separator like a comma prevents this.

  15. 15

    In tabulation for the 'can sum' problem, what is the initial value in all positions of the table except index 0?

    1. A

      null

    2. B

      false

    3. C

      0

    4. D

      true

    Correct answer
    B

    false

    Explanation

    The table is initialized with false values, assuming that no target sum (except 0) can be generated until proven otherwise.

  16. 16

    What is the space complexity of the memoized Fibonacci solution?

    1. A

      O(1)O(1)O(1)

    2. B

      O(n2)O(n^2)O(n2)

    3. C

      O(n)O(n)O(n)

    4. D

      O(2n)O(2^n)O(2n)

    Correct answer
    C

    O(n)O(n)O(n)

    Explanation

    The memoized Fibonacci uses a memo object that stores at most n entries (for each distinct n), so space complexity is O(n)O(n)O(n).

  17. 17

    In the grid traveler problem, how does the function change when moving downward?

    1. A

      Decrease the number of rows by 1

    2. B

      Increase the number of rows by 1

    3. C

      Decrease the number of columns by 1

    4. D

      Both dimensions decrease by 1

    Correct answer
    A

    Decrease the number of rows by 1

    Explanation

    Moving downward reduces the number of rows by 1, effectively shrinking the grid vertically. The number of columns stays the same.

  18. 18

    What is the key insight to optimize the recursive 'can construct' problem using memoization?

    1. A

      Using the length of the target as the key

    2. B

      Using the word bank array as the key

    3. C

      Using the target string as the key

    4. D

      Using the index of the word as the key

    Correct answer
    C

    Using the target string as the key

    Explanation

    Since the word bank does not change across recursive calls, the only changing argument is the target string. Therefore, the target string is used as the key in the memo object.

  19. 19

    In tabulation for 'how sum', what is the initial value at index 0 of the table?

    1. A

      null

    2. B

      true

    3. C

      0

    4. D

      An empty array

    Correct answer
    D

    An empty array

    Explanation

    Index 0 is seeded with an empty array, representing that the target sum 0 can be made by using no numbers.

  20. 20

    What is the time complexity of the tabulated 'can sum' solution?

    1. A

      O(m2)O(m^2)O(m2)

    2. B

      O(m+n)O(m + n)O(m+n)

    3. C

      O(m×n)O(m \times n)O(m×n)

    4. D

      O(2m+n)O(2^{m+n})O(2m+n)

    Correct answer
    C

    O(m×n)O(m \times n)O(m×n)

    Explanation

    The tabulated can sum has time complexity O(m×n)O(m \times n)O(m×n) because it iterates over m positions in the table and for each position, iterates over n numbers.

  21. 21

    When constructing the recursion tree for Fibonacci, what do the nodes represent?

    1. A

      The function calls with the argument n

    2. B

      The sum of previous two nodes

    3. C

      The Fibonacci numbers themselves

    4. D

      The base case values

    Correct answer
    A

    The function calls with the argument n

    Explanation

    Each node in the recursion tree represents a call to fib(n) with a specific n. The number inside the node is the argument n.

  22. 22

    In the 'best sum' problem, what is the space complexity of the tabulated solution?

    1. A

      O(2m)O(2^m)O(2m)

    2. B

      O(m)O(m)O(m)

    3. C

      O(m×n)O(m \times n)O(m×n)

    4. D

      O(m2)O(m^2)O(m2)

    Correct answer
    D

    O(m2)O(m^2)O(m2)

    Explanation

    The tabulated best sum maintains a table of size m, where each entry can be an array of length up to m, leading to O(m2)O(m^2)O(m2) space.

  23. 23

    What is the base case for the 'grid traveler' problem?

    1. A

      If m and n are both 0, return 1

    2. B

      If m or n is 0, return 1; if m and n are 1, return 0

    3. C

      If m or n is negative, return null

    4. D

      If m or n is 0, return 0; if m and n are 1, return 1

    Correct answer
    D

    If m or n is 0, return 0; if m and n are 1, return 1

    Explanation

    If either dimension is 0, the grid is empty and there are 0 ways. If both dimensions are 1, there is exactly 1 way (the start is the end).

  24. 24

    In the 'all construct' problem, what does the function return if the target string can be constructed?

    1. A

      A number representing the count of ways

    2. B

      An array of all possible ways, each way represented as an array of words

    3. C

      The shortest way as an array

    4. D

      A boolean

    Correct answer
    B

    An array of all possible ways, each way represented as an array of words

    Explanation

    The 'all construct' function returns a 2D array where each inner array is a combination of words that concatenate to form the target string.

  25. 25

    When using tabulation for 'can construct', what does an index i in the table represent?

    1. A

      The substring from index 0 to i-1 (prefix of length i)

    2. B

      The empty string

    3. C

      The entire target string

    4. D

      The substring from index i to the end

    Correct answer
    A

    The substring from index 0 to i-1 (prefix of length i)

    Explanation

    In the tabulation for string problems, index i represents the prefix of the target string of length i (i.e., characters from 0 to i-1). This allows representation of the empty string at index 0.

  26. 26

    What is the time complexity of the brute-force recursive 'all construct' solution?

    1. A

      Linear

    2. B

      Logarithmic

    3. C

      Exponential

    4. D

      Polynomial

    Correct answer
    C

    Exponential

    Explanation

    The 'all construct' problem requires returning all possible constructions, which can be exponential in number. The brute-force solution explores all combinations, leading to exponential time complexity.

  27. 27

    In the recursive 'how sum' function, what is the return type?

    1. A

      number

    2. B

      boolean

    3. C

      array or null

    4. D

      string

    Correct answer
    C

    array or null

    Explanation

    The 'how sum' function returns an array representing a combination of numbers that sum to the target, or null if no such combination exists.

  28. 28

    What is the main advantage of tabulation over memoization?

    1. A

      Tabulation uses less memory always.

    2. B

      Tabulation is easier to implement.

    3. C

      Tabulation is iterative and avoids recursion overhead.

    4. D

      Tabulation is always faster.

    Correct answer
    C

    Tabulation is iterative and avoids recursion overhead.

    Explanation

    Tabulation is an iterative bottom-up approach that avoids recursion overhead and stack overflow issues. However, it may not always be faster or use less memory.

  29. 29

    In the 'count construct' problem, how does the tabulation algorithm propagate values?

    1. A

      It sets the next indices to true.

    2. B

      It adds the value at the current index to the next indices based on word lengths.

    3. C

      It multiplies the value at the current index.

    4. D

      It copies the entire subarray to the next indices.

    Correct answer
    B

    It adds the value at the current index to the next indices based on word lengths.

    Explanation

    In count construct tabulation, when at index i, if the value is non-zero, it adds that value to index i + word.length for each matching word, because the number of ways to build the prefix extends to the longer prefix.

  30. 30

    What is a key difference between memoization and tabulation?

    1. A

      Memoization is always recursive; tabulation is always iterative.

    2. B

      Memoization is top-down; tabulation is bottom-up.

    3. C

      Memoization requires a base case; tabulation does not.

    4. D

      Memoization uses arrays; tabulation uses objects.

    Correct answer
    B

    Memoization is top-down; tabulation is bottom-up.

    Explanation

    Memoization is a top-down approach where we start from the original problem and recursively break it down, caching results. Tabulation is a bottom-up approach where we fill a table iteratively from the simplest subproblems.

  31. 31

    In the recursive brute-force 'can sum' function, what is the base case for a negative target sum?

    1. A

      Return 0

    2. B

      Return null

    3. C

      Return true

    4. D

      Return false

    Correct answer
    D

    Return false

    Explanation

    If the target sum becomes negative, it means we have overshot, so it is not possible to generate the target sum, and the function returns false.

  32. 32

    In the 'best sum' problem, how is the shortest combination determined?

    1. A

      By using a greedy approach

    2. B

      By always taking the largest number first

    3. C

      By comparing lengths of combinations and choosing the smaller one

    4. D

      By returning the first combination found

    Correct answer
    C

    By comparing lengths of combinations and choosing the smaller one

    Explanation

    The best sum algorithm explores all combinations and keeps track of the shortest one by comparing array lengths and updating the stored combination if a shorter one is found.

  33. 33

    What is the role of the 'spread operator' in the 'how sum' tabulation implementation?

    1. A

      To concatenate arrays

    2. B

      To remove elements from an array

    3. C

      To add an element to the end of an array

    4. D

      To create a copy of the array at the current index

    Correct answer
    D

    To create a copy of the array at the current index

    Explanation

    The spread operator is used to copy all elements from the array at the current index into a new array, which is then assigned to the forward-looking index after adding the current number.

  34. 34

    In the grid traveler problem, what is the space complexity of the tabulated solution?

    1. A

      O(m×n)O(m \times n)O(m×n)

    2. B

      O(2m+n)O(2^{m+n})O(2m+n)

    3. C

      O(max⁡(m,n))O(\max(m, n))O(max(m,n))

    4. D

      O(m+n)O(m + n)O(m+n)

    Correct answer
    A

    O(m×n)O(m \times n)O(m×n)

    Explanation

    The tabulated grid traveler uses a 2D table of size (m+1)×(n+1)(m+1) \times (n+1)(m+1)×(n+1), which requires O(m×n)O(m \times n)O(m×n) space.

  35. 35

    When using tabulation for 'all construct', what is the initial value at index 0 of the table?

    1. A

      An empty string

    2. B

      An array containing an empty array

    3. C

      null

    4. D

      An empty one-dimensional array

    Correct answer
    B

    An array containing an empty array

    Explanation

    Index 0 (representing the empty string) should be initialized with an array containing an empty array, indicating there is one way to construct the empty string: by using no words.

  36. 36

    What is the time complexity of the brute-force 'can construct' solution?

    1. A

      O(nm)O(n^m)O(nm)

    2. B

      O(n×m)O(n \times m)O(n×m)

    3. C

      O(2n+m)O(2^{n+m})O(2n+m)

    4. D

      O(mn)O(m^n)O(mn)

    Correct answer
    A

    O(nm)O(n^m)O(nm)

    Explanation

    The brute-force can construct has time complexity O(nm)O(n^m)O(nm), where n is the number of words and m is the length of the target string. Each recursive call can branch up to n ways, and the depth is up to m.

  37. 37

    In the 'can sum' problem, what does the tabulation table store?

    1. A

      Null or an array

    2. B

      The number of ways to achieve that sum

    3. C

      Booleans indicating if that target sum is possible

    4. D

      Arrays of numbers that sum to that target

    Correct answer
    C

    Booleans indicating if that target sum is possible

    Explanation

    In the boolean version (can sum), the table stores true/false values indicating whether the target sum at that index is achievable.

  38. 38

    What is the base case for the recursive 'can construct' function?

    1. A

      If target length is 1, return true

    2. B

      If target is empty, return false

    3. C

      If target is empty, return an empty array

    4. D

      If target is empty, return true

    Correct answer
    D

    If target is empty, return true

    Explanation

    If the target string is empty, it can be constructed by taking no words, so the function returns true.

  39. 39

    In the 'grid traveler' problem, what is the recurrence relation?

    1. A

      gridTraveller(m, n) = gridTraveller(m-1, n) + gridTraveller(m, n-1)

    2. B

      gridTraveller(m, n) = gridTraveller(m-1, n-1)

    3. C

      gridTraveller(m, n) = gridTraveller(m, n-1) - gridTraveller(m-1, n)

    4. D

      gridTraveller(m, n) = gridTraveller(m-1, n) * gridTraveller(m, n-1)

    Correct answer
    A

    gridTraveller(m, n) = gridTraveller(m-1, n) + gridTraveller(m, n-1)

    Explanation

    The number of ways to travel in an m x n grid is the sum of ways after moving down (reducing rows by 1) and moving right (reducing columns by 1).

  40. 40

    What is a common off-by-one error in tabulation?

    1. A

      Forgetting to initialize the table

    2. B

      Using table length equal to the input instead of input + 1

    3. C

      Using the wrong data type for the table

    4. D

      Starting the loop from index 1 instead of 0

    Correct answer
    B

    Using table length equal to the input instead of input + 1

    Explanation

    A frequent mistake is to create a table of size n (the input) instead of n+1, so that the last index corresponds to the input value and there is room for the base case at index 0.

  41. 41

    In the 'how sum' problem, what is the time complexity of the memoized solution?

    1. A

      O(2m)O(2^m)O(2m)

    2. B

      O(m×n)O(m \times n)O(m×n)

    3. C

      O(nm)O(n^m)O(nm)

    4. D

      O(m2×n)O(m^2 \times n)O(m2×n)

    Correct answer
    D

    O(m2×n)O(m^2 \times n)O(m2×n)

    Explanation

    The memoized how sum has time complexity O(m2×n)O(m^2 \times n)O(m2×n) because there are m distinct target sums, for each we iterate over n numbers, and copying the array (which can be of length up to m) takes O(m) time.

Question 1 of 41
Question 1 of 41

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent