CS Grade 9-12

CS: Dynamic Programming: Memoization and Tabulation

Solving overlapping subproblems with saved results

View Answer Key
Name:
Date:
Score: / 12

Solving overlapping subproblems with saved results

CS - Grade 9-12

Instructions: Read each problem carefully. Show your reasoning, tables, or code traces in the space provided.
  1. 1

    In your own words, explain what dynamic programming is and name the two main properties a problem usually needs in order for dynamic programming to be useful.

  2. 2
    A branching recursion tree with repeated colored subtrees showing duplicated work.

    A recursive Fibonacci function calls fib(n - 1) and fib(n - 2) until it reaches fib(0) or fib(1). Explain why this plain recursive version does repeated work.

  3. 3

    For the Fibonacci sequence where fib(0) = 0 and fib(1) = 1, fill in the tabulation values from fib(0) through fib(7).

  4. 4

    Compare memoization and tabulation. State one similarity and one difference between the two approaches.

  5. 5

    The following pseudocode uses memoization: function fib(n): if n is in memo: return memo[n] if n <= 1: return n memo[n] = fib(n - 1) + fib(n - 2) return memo[n] If fib(6) is called with an empty memo, what final key-value pairs should be stored in memo for n from 2 through 6?

  6. 6

    A student says, "Memoization always uses less memory than tabulation because it only computes what it needs." Explain why this statement is not always true.

  7. 7
    A staircase diagram with arrows showing one-step and two-step moves.

    You can climb a staircase by taking either 1 step or 2 steps at a time. Let ways(n) be the number of ways to climb n steps. Write the recurrence relation and base cases for this problem.

  8. 8
    A row of table cells with arrows from two earlier cells to the next, linked to a staircase model.

    Using the staircase recurrence ways(n) = ways(n - 1) + ways(n - 2), ways(0) = 1, and ways(1) = 1, compute ways(2) through ways(5) using tabulation.

  9. 9

    A coin change problem asks for the minimum number of coins needed to make a target amount using coin values 1, 3, and 4. Let dp[x] be the minimum number of coins needed for amount x. Write a recurrence for dp[x].

  10. 10

    Using coins 1, 3, and 4 with dp[0] = 0, find the minimum number of coins needed to make amount 6. Show the best combination.

  11. 11

    A longest common subsequence problem compares strings A = "ABC" and B = "AC". What is the length of the longest common subsequence, and what is one such subsequence?

  12. 12

    For Fibonacci, explain how a tabulation solution can be changed from using an array of size n + 1 to using only constant extra space.

LivePhysics™.com CS - Grade 9-12

More CS Worksheets

See all CS worksheets

More Grade 9-12 Worksheets

See all Grade 9-12 worksheets