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

Study methods

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

Dynamic Programming

  • 57Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Recursive Design

What is the central goal of the algorithmic-design approach introduced in the lecture?

57 questions

  1. 1

    What is the central goal of the algorithmic-design approach introduced in the lecture?

    1. A

      Converting every input into a graph and applying a shortest-path algorithm

    2. B

      Constructing an algorithm from scratch, often aiming for a polynomial-time solution

    3. C

      Replacing recursive algorithms with longer, problem-specific code

    4. D

      Selecting a data structure that has already been shown to solve the problem directly

    Correct answer
    A

    Converting every input into a graph and applying a shortest-path algorithm

    Explanation

    Algorithmic design focuses on constructing an algorithm from scratch, often seeking a polynomial-time solution, rather than only applying or adapting previously known algorithms.

  2. 2

    What makes an algorithm recursive in the broad sense described in the lecture?

    1. A

      It solves a problem by expressing it in terms of smaller subproblems

    2. B

      It stores every possible answer before examining the input

    3. C

      It avoids reusing code by creating separate instructions for every input size

    4. D

      It requires that every problem be represented as a graph

    Correct answer
    A

    It solves a problem by expressing it in terms of smaller subproblems

    Explanation

    Recursive design breaks a problem into smaller subproblems and specifies how to solve one problem using solutions to others.

  3. 3

    Which list correctly expands the SRTBOT design framework?

    1. A

      Subproblems, recursion, tables, boundaries, operations, and time

    2. B

      Sorting, recursion, traversal, branching, optimization, and termination

    3. C

      Subproblems, relations, topological order, base cases, original problem, and time

    4. D

      Subarrays, recurrence, testing, binary search, output, and transfer

    Correct answer
    C

    Subproblems, relations, topological order, base cases, original problem, and time

    Explanation

    SRTBOT stands for subproblems, relations, topological order, base cases, original problem, and time analysis.

  4. 4

    Which step is often the hardest part of designing a recursive algorithm?

    1. A

      Ensure that every possible subset of the input is a subproblem

    2. B

      Choose the fastest programming language for implementing the recurrence

    3. C

      Define a useful collection of subproblems, usually polynomial in number

    4. D

      Find a closed-form solution before considering smaller problems

    Correct answer
    C

    Define a useful collection of subproblems, usually polynomial in number

    Explanation

    Choosing the right subproblems is often the hardest part of recursive algorithm design. A useful design typically has only polynomially many subproblems.

  5. 5

    In SRTBOT, what does the relation specify?

    1. A

      A rule for solving one subproblem using solutions to other subproblems

    2. B

      An estimate of the memory required to store the final answer

    3. C

      A list of all input values that should be ignored

    4. D

      A proof that the original problem has a unique solution

    Correct answer
    A

    A rule for solving one subproblem using solutions to other subproblems

    Explanation

    The relation specifies how to compute one subproblem using solutions to other subproblems; those dependencies define the recursive structure.

  6. 6

    What purpose does a topological order serve in a recursive algorithm’s subproblem graph?

    1. A

      An order in which every subproblem is computed after its dependencies

    2. B

      An order that guarantees the largest subproblem is solved first

    3. C

      An order based only on the amount of time each subproblem takes

    4. D

      An order that groups subproblems by their input values

    Correct answer
    A

    An order in which every subproblem is computed after its dependencies

    Explanation

    A topological order arranges subproblems so that each dependency is solved before the problem that relies on it.

  7. 7

    Why should the dependency graph of recursive subproblems be acyclic?

    1. A

      It ensures that each subproblem has exactly two dependencies

    2. B

      It prevents recursive calls from continuing indefinitely through cyclic dependencies

    3. C

      It guarantees that every subproblem has the same running time

    4. D

      It ensures that the recurrence uses only addition and subtraction

    Correct answer
    B

    It prevents recursive calls from continuing indefinitely through cyclic dependencies

    Explanation

    If recursive dependencies contain a cycle, the algorithm can keep calling subproblems in that cycle without terminating. A directed acyclic graph avoids this issue.

  8. 8

    How can specifying a topological order help establish that a recursive algorithm is well-founded?

    1. A

      It replaces the need to establish a recurrence relation

    2. B

      It ensures that the original problem is always the first subproblem solved

    3. C

      It allows the algorithm to skip defining base cases

    4. D

      It gives an order in which dependencies are ready before they are used

    Correct answer
    D

    It gives an order in which dependencies are ready before they are used

    Explanation

    A valid topological order provides a way to compute dependencies before the subproblems that need them. It may be specified explicitly, such as through a loop.

  9. 9

    How are merge sort’s subproblems and original problem defined in the lecture’s notation?

    1. A

      A subproblem merges the full array; the original problem sorts each item separately

    2. B

      A subproblem selects one item from the array; the original problem selects the median

    3. C

      A subproblem sorts a prefix; the original problem sorts only the final item

    4. D

      A subproblem sorts the range from iii to j−1j-1j−1; the original problem is to sort the range from 000 to n−1n-1n−1

    Correct answer
    D

    A subproblem sorts the range from iii to j−1j-1j−1; the original problem is to sort the range from 000 to n−1n-1n−1

    Explanation

    For merge sort, a subproblem sorts the range from index iii up to but not including jjj. The original task is the range from 000 to nnn.

  10. 10

    How does the merge-sort recurrence solve the subproblem for range [i,j)[i,j)[i,j)?

    1. A

      It sorts the whole range first and then recursively sorts individual elements

    2. B

      It sorts both smaller ranges and merges their sorted outputs

    3. C

      It merges the unsorted halves and then sorts the merged result

    4. D

      It sorts one half and discards the other half before returning the result

    Correct answer
    B

    It sorts both smaller ranges and merges their sorted outputs

    Explanation

    Merge sort divides a range at a midpoint, recursively sorts both halves, and merges the two sorted results to obtain the sorted range.

  11. 11

    Which topological order is suitable for merge sort’s range subproblems?

    1. A

      Increasing starting index, because both halves begin at larger indices

    2. B

      Decreasing range length, because the largest ranges are base cases

    3. C

      Increasing ending index only, regardless of range length

    4. D

      Increasing range length, so the smaller halves are solved first

    Correct answer
    D

    Increasing range length, so the smaller halves are solved first

    Explanation

    For merge sort, subproblems can be ordered by increasing range length j−ij-ij−i. A non-base range’s two halves are strictly shorter, so they come earlier in that order.

  12. 12

    What is a valid merge-sort base case described in the lecture?

    1. A

      Any range whose midpoint is its first element

    2. B

      The empty range, which is already sorted

    3. C

      A range with two elements, which must be merged recursively

    4. D

      The full input range, which is assumed to be sorted

    Correct answer
    B

    The empty range, which is already sorted

    Explanation

    The empty range is a base case for sorting: there are no elements that need to be ordered.

  13. 13

    What is memoization?

    1. A

      Reordering recursive calls so that the largest one runs first

    2. B

      Recording every path explored by the algorithm, whether needed or not

    3. C

      Storing subproblem solutions so repeated calls can reuse them

    4. D

      Replacing each recurrence with a closed-form equation

    Correct answer
    C

    Storing subproblem solutions so repeated calls can reuse them

    Explanation

    Memoization records computed subproblem answers and reuses them when the same subproblem is encountered again.

  14. 14

    What information does a memo table store?

    1. A

      A data structure mapping solved subproblems to their solutions

    2. B

      A stack containing every recursive call, including completed repeats

    3. C

      A graph mapping each base case to all possible inputs

    4. D

      A queue mapping input elements to the order they were read

    Correct answer
    A

    A data structure mapping solved subproblems to their solutions

    Explanation

    A memo can be implemented with a dictionary that maps subproblem specifications to their computed solutions. Arrays or hash tables can serve this role when appropriate.

  15. 15

    Why does the straightforward recursive Fibonacci algorithm take exponential time?

    1. A

      It performs a linear number of recursive calls, each taking exponential time

    2. B

      It uses too many distinct subproblems, even though each is solved once

    3. C

      It spends exponential time sorting the Fibonacci values

    4. D

      It recomputes the same Fibonacci subproblems many times

    Correct answer
    D

    It recomputes the same Fibonacci subproblems many times

    Explanation

    Naive recursive Fibonacci repeatedly computes the same values, creating an exponentially large recursion tree even though there are only linearly many distinct indices.

  16. 16

    Which recurrence and base cases define the Fibonacci numbers in the lecture?

    1. A

      fi=fi−1fi−2f_i=f_{i-1}f_{i-2}fi​=fi−1​fi−2​, with f1=f2=0f_1=f_2=0f1​=f2​=0

    2. B

      fi=fi−1+fi−2f_i=f_{i-1}+f_{i-2}fi​=fi−1​+fi−2​, with f1=f2=1f_1=f_2=1f1​=f2​=1

    3. C

      fi=2fi−1f_i=2f_{i-1}fi​=2fi−1​, with f1=1f_1=1f1​=1

    4. D

      fi=fi−1−fi−2f_i=f_{i-1}-f_{i-2}fi​=fi−1​−fi−2​, with f1=f2=1f_1=f_2=1f1​=f2​=1

    Correct answer
    B

    fi=fi−1+fi−2f_i=f_{i-1}+f_{i-2}fi​=fi−1​+fi−2​, with f1=f2=1f_1=f_2=1f1​=f2​=1

    Explanation

    The Fibonacci recurrence is fi=fi−1+fi−2f_i=f_{i-1}+f_{i-2}fi​=fi−1​+fi−2​, with the stated base cases f1=f2=1f_1=f_2=1f1​=f2​=1.

  17. 17

    Why does memoization reduce the number of Fibonacci computations to a linear number of subproblems?

    1. A

      Each of the nnn distinct indexed subproblems is computed at most once

    2. B

      Each Fibonacci number is calculated separately for every path in the call tree

    3. C

      The algorithm stores only the final answer and recomputes all earlier values

    4. D

      The recursive calls are eliminated by solving the recurrence algebraically

    Correct answer
    A

    Each of the nnn distinct indexed subproblems is computed at most once

    Explanation

    There are only nnn Fibonacci subproblems with indices from 111 to nnn. With memoization, each is computed at most once, and each computation uses constant-time arithmetic in the usual unit-cost model.

  18. 18

    What does a memoized function do when its requested subproblem is already in the memo?

    1. A

      It discards stored values whenever a new call begins

    2. B

      It recursively computes every dependency before checking storage

    3. C

      It checks the memo only after recomputing the subproblem

    4. D

      It checks the memo first and returns the stored answer if present

    Correct answer
    D

    It checks the memo first and returns the stored answer if present

    Explanation

    A memoized recursive function first checks whether its subproblem is already stored. If so, it returns the saved answer without making recursive calls again.

  19. 19

    In the memoized Fibonacci recursion, why can many calls on the right side of the recursion tree finish immediately?

    1. A

      The second request for fi−2f_{i-2}fi−2​ can return its already-computed value

    2. B

      The recurrence no longer needs the value of fi−2f_{i-2}fi−2​

    3. C

      The call for fi−2f_{i-2}fi−2​ is replaced by a call for fi+2f_{i+2}fi+2​

    4. D

      The base case automatically determines every larger Fibonacci number

    Correct answer
    A

    The second request for fi−2f_{i-2}fi−2​ can return its already-computed value

    Explanation

    When computing fif_ifi​, the recursive call for fi−1f_{i-1}fi−1​ will compute fi−2f_{i-2}fi−2​ as part of its work. The later request for fi−2f_{i-2}fi−2​ can therefore use the saved result.

  20. 20

    Which statement best describes possible implementations of a memo table?

    1. A

      The memo is an interface that can be implemented with an array or a hash table

    2. B

      The memo must use a sorted tree because subproblems are computed in order

    3. C

      A memo must always be a linked list of recursive calls

    4. D

      A memo is a special-purpose graph that stores only base cases

    Correct answer
    A

    The memo is an interface that can be implemented with an array or a hash table

    Explanation

    A dictionary can provide the memo interface, while a direct-access array is often suitable when subproblem identifiers are integer indices. A hash table is another option.

  21. 21

    How should the running time of a memoized dynamic program generally be analyzed?

    1. A

      Sum the nonrecursive work over all distinct subproblems

    2. B

      Count only the base cases and ignore all other subproblems

    3. C

      Multiply the number of recursive calls by the depth of the recursion tree

    4. D

      Count the number of lines in the recursive function

    Correct answer
    A

    Sum the nonrecursive work over all distinct subproblems

    Explanation

    Once a subproblem is solved, memoization prevents solving it again. The running time can therefore be bounded by summing the nonrecursive work for each distinct subproblem.

  22. 22

    Why does memoization not improve merge sort in the way it improves naive recursive Fibonacci?

    1. A

      Merge sort’s memo must store every possible permutation

    2. B

      Merge sort has no repeated subproblems to reuse, so memoization offers no benefit

    3. C

      Merge sort cannot be written recursively, so it cannot use a memo

    4. D

      Merge sort’s base cases are exponential in number

    Correct answer
    B

    Merge sort has no repeated subproblems to reuse, so memoization offers no benefit

    Explanation

    Memoization does not create repeated subproblems where none existed. Merge sort’s recursive ranges are disjoint, so storing their answers does not provide the same benefit it provides for Fibonacci.

  23. 23

    What is the subproblem used for the DAG shortest-path dynamic program?

    1. A

      The shortest path from each vertex to a different source

    2. B

      The longest path between every pair of vertices in the graph

    3. C

      The shortest path from every vertex to itself, for one selected vertex

    4. D

      The shortest-path distance from the source sss to a vertex vvv

    Correct answer
    D

    The shortest-path distance from the source sss to a vertex vvv

    Explanation

    The DAG shortest-path subproblem is the shortest-path distance from a fixed source sss to a particular vertex vvv. Solving it for all vertices gives the single-source result.

  24. 24

    How does the recurrence compute the shortest-path distance from sss to vvv?

    1. A

      Take the minimum over incoming edges uvuvuv of the distance to uuu plus the weight of uvuvuv

    2. B

      Add the weights of all incoming edges to the distance from sss to vvv

    3. C

      Choose the incoming edge with the smallest weight, regardless of the distance to its tail

    4. D

      Take the maximum over outgoing edges vuvuvu of the distance to uuu minus the weight of vuvuvu

    Correct answer
    A

    Take the minimum over incoming edges uvuvuv of the distance to uuu plus the weight of uvuvuv

    Explanation

    Any path ending at vvv either begins at sss when v=sv=sv=s, or reaches vvv through an incoming edge uvuvuv. Thus the best distance is the minimum distance to a predecessor plus the weight of its final edge.

  25. 25

    Which edges does the DAG shortest-path recurrence examine for a given vertex vvv?

    1. A

      Incoming edges to vvv, because a path reaching vvv must enter from a predecessor

    2. B

      Outgoing edges from vvv, because the path must continue after reaching vvv

    3. C

      All edges in the graph, because every edge may be part of the final step

    4. D

      Only edges incident to the source, because all paths start there

    Correct answer
    A

    Incoming edges to vvv, because a path reaching vvv must enter from a predecessor

    Explanation

    The recurrence examines incoming edges to vvv. For each predecessor uuu, it considers the best path to uuu followed by edge uvuvuv.

  26. 26

    How does the recurrence account for a vertex that is unreachable from the source?

    1. A

      Repeat the recurrence until an unreachable vertex becomes reachable

    2. B

      Include infinity among the candidates when no path reaches the vertex

    3. C

      Assign a distance of zero to every unreachable vertex

    4. D

      Use the largest edge weight as the distance to every unreachable vertex

    Correct answer
    B

    Include infinity among the candidates when no path reaches the vertex

    Explanation

    If no path from sss to vvv exists, the minimum should represent that no finite candidate distance is available; the recurrence includes infinity for this possibility.

  27. 27

    Why can DAG shortest-path subproblems be computed in a valid topological order?

    1. A

      A topological order of the DAG makes the dependency computation acyclic

    2. B

      The subproblems are ordered by increasing distance, which is known in advance

    3. C

      A topological order is unnecessary because all distances are base cases

    4. D

      The algorithm sorts vertices by their incoming-edge weights

    Correct answer
    A

    A topological order of the DAG makes the dependency computation acyclic

    Explanation

    A DAG has a topological order, and the graph’s acyclicity provides the order needed to compute predecessor subproblems before dependent ones.

  28. 28

    What is the base case for the DAG shortest-path recurrence?

    1. A

      The distance from every vertex to sss is 000

    2. B

      The distance from sss to itself is infinity

    3. C

      The distance from sss to every vertex is the weight of its first edge

    4. D

      The distance from sss to sss is 000

    Correct answer
    D

    The distance from sss to sss is 000

    Explanation

    The base case is the source’s distance to itself, which is 000.

  29. 29

    Why can the DAG shortest-path dynamic program run in O(V+E)O(V+E)O(V+E) time?

    1. A

      The algorithm takes O(V2)O(V^2)O(V2) time because every pair of vertices is compared

    2. B

      The total work is O(V+E)O(V+E)O(V+E) because incoming-edge scans sum to the number of edges

    3. C

      The algorithm takes O(VE)O(VE)O(VE) time because every edge is scanned for every vertex

    4. D

      The algorithm takes O(E2)O(E^2)O(E2) time because every edge is compared to every other edge

    Correct answer
    B

    The total work is O(V+E)O(V+E)O(V+E) because incoming-edge scans sum to the number of edges

    Explanation

    For each vertex, the recurrence scans its incoming edges. Summed across all vertices, this examines each edge once, along with per-vertex work, yielding O(V+E)O(V+E)O(V+E) time.

  30. 30

    How is the recurrence-based DAG shortest-path algorithm related to DAG relaxation?

    1. A

      It avoids examining edge weights by using only the number of vertices

    2. B

      It computes the same minima as DAG relaxation, with the computation organized differently

    3. C

      It performs memoized recursive calls but never processes a vertex twice

    4. D

      It uses a different recurrence that maximizes over outgoing edges

    Correct answer
    B

    It computes the same minima as DAG relaxation, with the computation organized differently

    Explanation

    DAG relaxation processes vertices in topological order and relaxes their edges. This is equivalent to computing the minimum over candidate predecessor paths, whether done all at once or through successive updates.

  31. 31

    What graph-search behavior is implicit in a generic memoized recursive dynamic program?

    1. A

      It is a breadth-first search that ignores the dependency graph

    2. B

      It performs a depth-first traversal of the subproblem dependencies, using the memo to avoid repeats

    3. C

      It is a random traversal that depends on the order edges are stored

    4. D

      It is a sorting algorithm that replaces shortest-path relaxation

    Correct answer
    B

    It performs a depth-first traversal of the subproblem dependencies, using the memo to avoid repeats

    Explanation

    The memoized recursive version follows dependencies in a depth-first manner, and the memo records which subproblems have already been visited. The dependency order gives the needed computation order.

  32. 32

    Which are natural polynomial-size subproblem families for sequence inputs?

    1. A

      Only single elements and arbitrary sets of elements

    2. B

      All possible reorderings and all partitions of the sequence

    3. C

      Subsequences, permutations, and all subsets

    4. D

      Prefixes, suffixes, and substrings

    Correct answer
    D

    Prefixes, suffixes, and substrings

    Explanation

    For a sequence of length nnn, there are only linearly many prefixes and suffixes, and quadratically many substrings. These provide polynomial-size subproblem families.

  33. 33

    What distinguishes a substring from a subsequence?

    1. A

      A substring and a subsequence both require consecutive items

    2. B

      A subsequence is always shorter than two items, while a substring is not

    3. C

      A substring uses consecutive items, while a subsequence may omit items in the middle

    4. D

      A substring may omit middle items, while a subsequence must be consecutive

    Correct answer
    C

    A substring uses consecutive items, while a subsequence may omit items in the middle

    Explanation

    A substring consists of consecutive items between two endpoints. A subsequence may omit items in the middle, so there can be exponentially many subsequences.

  34. 34

    Why are arbitrary subsequences usually a poor choice of subproblems for a polynomial-time dynamic program?

    1. A

      There are 2n2^n2n subsequences because each item can be included or omitted

    2. B

      There are n!n!n! subsequences because their items can be reordered

    3. C

      There are nnn subsequences because each starts at a different index

    4. D

      There are n2n^2n2 subsequences because each has two endpoints

    Correct answer
    A

    There are 2n2^n2n subsequences because each item can be included or omitted

    Explanation

    Each of the nnn positions can independently be included or excluded in a subsequence, giving 2n2^n2n possible subsequences. This is generally too many subproblems for an efficient dynamic program.

  35. 35

    How do the numbers of prefix, suffix, and substring subproblems compare for a length-nnn sequence?

    1. A

      Prefixes and suffixes are linear in number, while substrings are quadratic

    2. B

      Suffixes are always better because they require no recurrence

    3. C

      Substrings are always better because they encode more information

    4. D

      Prefixes are exponential, while suffixes are quadratic

    Correct answer
    A

    Prefixes and suffixes are linear in number, while substrings are quadratic

    Explanation

    Prefixes and suffixes each give a linear number of subproblems, while substrings give a quadratic number. Prefixes or suffixes are often preferable when either works.

  36. 36

    What does the bowling subproblem b(i)b(i)b(i) represent?

    1. A

      The score from hitting pin iii alone, regardless of later pins

    2. B

      The number of ways to pair pins in the suffix beginning at iii

    3. C

      The minimum score obtainable using pins 000 through iii

    4. D

      The maximum score obtainable using pins iii through n−1n-1n−1

    Correct answer
    D

    The maximum score obtainable using pins iii through n−1n-1n−1

    Explanation

    For the bowling problem, b(i)b(i)b(i) is the maximum score obtainable from the suffix beginning at pin iii and continuing through the last pin.

  37. 37

    Which bowling subproblem gives the answer for the entire input?

    1. A

      b(n)b(n)b(n), because it contains every pin

    2. B

      b(1)b(1)b(1), because the first pin is excluded

    3. C

      The maximum of all individual values viv_ivi​

    4. D

      b(0)b(0)b(0), because it represents the full sequence of pins

    Correct answer
    D

    b(0)b(0)b(0), because it represents the full sequence of pins

    Explanation

    The suffix beginning at index 000 contains all pins, so its optimal score is the answer to the original bowling problem.

  38. 38

    What are the three choices considered for the first pin in the bowling recurrence?

    1. A

      Hit pin iii twice, skip pin i+1i+1i+1, or pair all remaining pins

    2. B

      Skip pin iii, pair it with any later pin, or hit every remaining pin together

    3. C

      Hit pin iii alone, pair it with pin i−1i-1i−1, or pair it with pin i+2i+2i+2

    4. D

      Skip pin iii, hit it alone, or pair it with pin i+1i+1i+1

    Correct answer
    D

    Skip pin iii, hit it alone, or pair it with pin i+1i+1i+1

    Explanation

    The first pin can be skipped, hit alone for viv_ivi​ points, or paired with the next pin for vivi+1v_i v_{i+1}vi​vi+1​ points. Each choice leaves a smaller suffix to solve.

  39. 39

    What expression corresponds to skipping pin iii in the bowling recurrence?

    1. A

      b(i+2)+vib(i+2)+v_ib(i+2)+vi​

    2. B

      b(i+1)+vivi+1b(i+1)+v_i v_{i+1}b(i+1)+vi​vi+1​

    3. C

      b(i+1)b(i+1)b(i+1)

    4. D

      b(i−1)b(i-1)b(i−1)

    Correct answer
    C

    b(i+1)b(i+1)b(i+1)

    Explanation

    Skipping pin iii leaves the suffix starting at i+1i+1i+1, so the score for that choice is b(i+1)b(i+1)b(i+1).

  40. 40

    What expression corresponds to hitting pin iii alone?

    1. A

      b(i+1)+vivi+1b(i+1)+v_i v_{i+1}b(i+1)+vi​vi+1​

    2. B

      b(i+2)+vib(i+2)+v_ib(i+2)+vi​

    3. C

      b(i)+vivi+1b(i)+v_i v_{i+1}b(i)+vi​vi+1​

    4. D

      b(i+1)+vib(i+1)+v_ib(i+1)+vi​

    Correct answer
    D

    b(i+1)+vib(i+1)+v_ib(i+1)+vi​

    Explanation

    If pin iii is hit alone, it contributes viv_ivi​ points and leaves the suffix beginning at i+1i+1i+1, giving b(i+1)+vib(i+1)+v_ib(i+1)+vi​.

  41. 41

    What expression corresponds to pairing pins iii and i+1i+1i+1?

    1. A

      b(i)+vivi+1b(i)+v_i v_{i+1}b(i)+vi​vi+1​

    2. B

      b(i+1)+vi+vi+1b(i+1)+v_i+v_{i+1}b(i+1)+vi​+vi+1​

    3. C

      b(i+2)+vivi+1b(i+2)+v_i v_{i+1}b(i+2)+vi​vi+1​

    4. D

      b(i+2)+vib(i+2)+v_ib(i+2)+vi​

    Correct answer
    C

    b(i+2)+vivi+1b(i+2)+v_i v_{i+1}b(i+2)+vi​vi+1​

    Explanation

    Pairing pins iii and i+1i+1i+1 gives their product as points, after which the remaining suffix starts at i+2i+2i+2.

  42. 42

    How should the bowling recurrence combine its available choices?

    1. A

      Add all three choices because each action can be performed together

    2. B

      Take the minimum of the three choices, including a pair beyond the sequence

    3. C

      Take the maximum of the valid choices and omit pairing when fewer than two pins remain

    4. D

      Choose the pair option whenever either pin has a positive value

    Correct answer
    C

    Take the maximum of the valid choices and omit pairing when fewer than two pins remain

    Explanation

    The recurrence takes the maximum of the scores from skipping, hitting alone, or pairing the first two pins. The pairing option is available only when a next pin exists.

  43. 43

    What is the bowling dynamic program’s base case?

    1. A

      b(n)=-, because no pins remain to score points

    2. B

      b(n)=0b(n)=0b(n)=0, because the empty suffix yields no points

    3. C

      b(n)=1b(n)=1b(n)=1, because the empty suffix has one possible action

    4. D

      b(n)=vnb(n)=v_nb(n)=vn​, because the final pin is always hit

    Correct answer
    B

    b(n)=0b(n)=0b(n)=0, because the empty suffix yields no points

    Explanation

    The base case is the empty suffix at index nnn. With no pins remaining, no points can be scored, so b(n)=0b(n)=0b(n)=0.

  44. 44

    Why does the bowling dynamic program run in linear time?

    1. A

      There are O(2n)O(2^n)O(2n) possible bowling strategies and each takes constant time

    2. B

      There are O(n2)O(n^2)O(n2) substring subproblems and O(n)O(n)O(n) work per subproblem

    3. C

      There are O(n)O(n)O(n) subproblems and each requires scanning all remaining pins

    4. D

      There are O(n)O(n)O(n) suffix subproblems and O(1)O(1)O(1) nonrecursive work per subproblem

    Correct answer
    D

    There are O(n)O(n)O(n) suffix subproblems and O(1)O(1)O(1) nonrecursive work per subproblem

    Explanation

    Computing each bowling subproblem requires only a constant number of arithmetic operations and comparisons, and there are linearly many suffixes, so the total time is linear.

  45. 45

    What topological order is used to compute the bowling suffix subproblems?

    1. A

      Increasing iii, because the full sequence is the empty suffix

    2. B

      Increasing suffix length, starting with the full sequence

    3. C

      Decreasing iii, so each suffix depends on already-computed shorter suffixes

    4. D

      In arbitrary order, because every suffix is independent

    Correct answer
    C

    Decreasing iii, so each suffix depends on already-computed shorter suffixes

    Explanation

    For suffix subproblems, the suffix beginning at nnn is solved first, followed by decreasing starting indices, ending with the full suffix at 000.

  46. 46

    How does bottom-up dynamic programming compute the bowling solution?

    1. A

      It stores only b(0)b(0)b(0) and computes all other values whenever needed

    2. B

      It initializes b(n)b(n)b(n), fills values from n−1n-1n−1 down to 000, and returns b(0)b(0)b(0)

    3. C

      It starts with b(0)b(0)b(0) and repeatedly replaces it with smaller scores

    4. D

      It recursively calls itself, then saves the answers after returning

    Correct answer
    B

    It initializes b(n)b(n)b(n), fills values from n−1n-1n−1 down to 000, and returns b(0)b(0)b(0)

    Explanation

    The bottom-up version initializes the base case, fills the table in topological order, applies the recurrence at each index, and returns b(0)b(0)b(0).

  47. 47

    Which local viewpoint often helps derive recurrences for suffix and prefix subproblems?

    1. A

      For suffixes, focus on the first item or items; for prefixes, focus on the last

    2. B

      For suffixes, focus on the last item; for prefixes, focus on the first

    3. C

      For both prefixes and suffixes, focus only on the middle item

    4. D

      For suffixes, focus on every item at once; for prefixes, focus on no items

    Correct answer
    A

    For suffixes, focus on the first item or items; for prefixes, focus on the last

    Explanation

    For a suffix, considering the first item or first few items creates choices that leave a smaller suffix. For a prefix, the analogous focus is usually its last item.

  48. 48

    What can happen when an item is removed from the middle of a substring subproblem?

    1. A

      Removing an endpoint always creates an arbitrary subsequence

    2. B

      Removing an interior item necessarily creates an exponential number of subproblems

    3. C

      Removing any item makes the remaining substring nonconsecutive

    4. D

      Removing an interior item can split it into two substrings

    Correct answer
    D

    Removing an interior item can split it into two substrings

    Explanation

    Removing an item from the middle of a substring can leave two substrings, which may become smaller subproblems.

  49. 49

    What is the key reasoning step in deriving many dynamic-programming recurrences?

    1. A

      Enumerate every complete solution and select the first one found

    2. B

      Avoid defining subproblems until after the algorithm has been implemented

    3. C

      Identify a useful feature or choice that reduces the problem to smaller subproblems

    4. D

      Choose the largest subproblem and assume its answer determines all others

    Correct answer
    C

    Identify a useful feature or choice that reduces the problem to smaller subproblems

    Explanation

    The design idea is to identify a feature or choice that, once fixed, reduces the task to a smaller subproblem. The recurrence considers the possible choices and combines their results.

  50. 50

    How are alternatives commonly combined in maximization and minimization dynamic programs?

    1. A

      Use an average whenever the alternatives have different values

    2. B

      Use a maximum for maximization problems and a minimum for minimization problems

    3. C

      Use a minimum for both maximization and minimization problems

    4. D

      Use a sum when maximizing and a product when minimizing

    Correct answer
    B

    Use a maximum for maximization problems and a minimum for minimization problems

    Explanation

    For optimization problems, if the alternatives represent competing valid choices, maximize when seeking the greatest value and minimize when seeking the least value.

  51. 51

    What does the lecture mean by using “local brute force” in dynamic programming?

    1. A

      It avoids examining alternatives by predicting the optimal choice in advance

    2. B

      It tries local alternatives while reusing shared subproblem solutions rather than recomputing them

    3. C

      It uses memoization to make every possible subproblem constant-time, including setup

    4. D

      It explores every complete strategy but removes recursion from the code

    Correct answer
    B

    It tries local alternatives while reusing shared subproblem solutions rather than recomputing them

    Explanation

    Dynamic programming can explore several local choices at each step without enumerating every complete combination, because repeated smaller subproblems are solved once and reused.

  52. 52

    Under what conditions does the standard memoized subproblem analysis give a polynomial-time algorithm?

    1. A

      Polynomially many subproblems with polynomial work per subproblem yield a polynomial-time bound

    2. B

      Polynomially many recursive calls guarantee polynomial time even without reuse

    3. C

      Any recurrence with a base case runs in polynomial time

    4. D

      A polynomial-time algorithm must have exactly one subproblem per input item

    Correct answer
    A

    Polynomially many subproblems with polynomial work per subproblem yield a polynomial-time bound

    Explanation

    If the number of subproblems is polynomial and the nonrecursive work for each one is polynomial, their total is polynomial, assuming memoization avoids repeated computation.

  53. 53

    Why does the lecture qualify the usual constant-time assumption for Fibonacci additions?

    1. A

      Fibonacci values fit in one bit, so additions are constant-time for all nnn

    2. B

      Fibonacci values grow exponentially and require Theta(n) bits, so large-number additions can cost more than constant time

    3. C

      The number of Fibonacci subproblems is exponential, so bit length is irrelevant

    4. D

      The word RAM model forbids addition on values larger than the input length

    Correct answer
    B

    Fibonacci values grow exponentially and require Theta(n) bits, so large-number additions can cost more than constant time

    Explanation

    For a fixed-width word RAM, arithmetic on word-sized values is often treated as constant-time. Fibonacci values have Theta(n) bits, so their addition may require more than constant time when the values exceed the word size.

  54. 54

    What effect can accounting for the bit lengths of Fibonacci numbers have on the memoized running-time bound?

    1. A

      The memoized algorithm remains exponential because the Fibonacci values grow

    2. B

      The memoized algorithm is polynomial, with bit costs potentially adding a term on the order of n2/wn^2/wn2/w

    3. C

      The memoized algorithm takes O(n\,\log n) because it sorts the values

    4. D

      The memoized algorithm is constant-time because the memo stores all answers

    Correct answer
    B

    The memoized algorithm is polynomial, with bit costs potentially adding a term on the order of n2/wn^2/wn2/w

    Explanation

    With nnn distinct Fibonacci subproblems and additions on values of Theta(n) bits, the bit-operation discussion gives a polynomial bound of n+Theta(n^2/w), rather than the exponential time of naive recursion.

  55. 55

    Why can applying the general memoized subproblem bound to merge sort produce a loose O(n3)O(n^3)O(n3) estimate?

    1. A

      The total is Theta(n\log n) because every range is merged once

    2. B

      Memoization proves merge sort takes O(n)O(n)O(n) time

    3. C

      A coarse sum gives an O(n3)O(n^3)O(n3) bound, although the actual merge-sort time is Theta(n\log n)

    4. D

      The range count is exponential, so the bound is exponential

    Correct answer
    C

    A coarse sum gives an O(n3)O(n^3)O(n3) bound, although the actual merge-sort time is Theta(n\log n)

    Explanation

    The number of possible merge-sort ranges is O(n2)O(n^2)O(n2), and merging a range can take time proportional to its length. Summing over all ranges gives an upper bound of O(n3)O(n^3)O(n3), which is valid but weaker than merge sort’s actual Theta(n\log n) time.

  56. 56

    What does “nonrecursive work” mean when analyzing a memoized recurrence?

    1. A

      It measures the time needed to write the recurrence on paper

    2. B

      It excludes recursive-call work, which is counted through the other subproblems

    3. C

      It includes the total running time of every recursive descendant

    4. D

      It counts only the time spent in base cases

    Correct answer
    B

    It excludes recursive-call work, which is counted through the other subproblems

    Explanation

    The recurrence’s nonrecursive work is the work performed for the current subproblem excluding the recursive computations. Those recursive subproblems are accounted for separately in the sum.

  57. 57

    How does bottom-up dynamic programming differ from the memoized recursive approach?

    1. A

      Memoization stores only base cases, while bottom-up methods store all answers

    2. B

      Memoization uses recursive calls and a memo, while bottom-up methods fill values in an explicit order

    3. C

      Memoization requires iteration, while bottom-up methods require recursion

    4. D

      Memoization and bottom-up dynamic programming cannot implement the same recurrence

    Correct answer
    B

    Memoization uses recursive calls and a memo, while bottom-up methods fill values in an explicit order

    Explanation

    Memoized recursion stores answers and returns them on later requests. Bottom-up dynamic programming instead fills a table explicitly in topological order, often using a loop and avoiding recursive-call overhead.

Question 1 of 57
Question 1 of 57

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent