Dynamic Programming Problems: Worked Tables for NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

Dynamic programming (DP) questions give you a small instance of a classic problem, such as Fibonacci, 0/1 knapsack, longest common subsequence (LCS) or coin change, and ask for the optimal value, one table cell, the chosen items or the number of recursive calls. You answer them by filling a table in the right order, so the skill being tested is setting up the recurrence correctly. Data Structures & Algorithms carries 10% of the NSCT according to HEC's Areas of Competencies, and 2026 candidates averaged only 40.2% accuracy in it, per the HEC NSCT report and APP.

This page goes deeper than the DP section of Algorithm Design Techniques MCQs, which explains what DP is and how it differs from greedy and divide and conquer. Here every table is filled in full. All of them were computed in Python before being written.

The method, step by step

  1. Define the state in words: "K[i][w] is the best value using the first i items with capacity w". If you cannot say it in one sentence, the table will go wrong.
  2. Write the recurrence, usually a choice: take or skip, match or skip, use this coin or not.
  3. Fix the base cases: row 0 and column 0 are usually 0, except ways-counting, where ways[0] = 1.
  4. Choose the fill order so every cell's dependencies are filled first: left to right and top to bottom for 2-D tables.
  5. Read the answer from the final cell, and trace back through the choices if the question asks which items or characters were used.

Fibonacci: recursion, memoisation and tabulation

Fibonacci is the simplest example of overlapping subproblems: the same values are needed again and again.

Worked example. Compare three ways of computing fib(6) = 8, where fib(n) = fib(n - 1) + fib(n - 2), fib(0) = 0 and fib(1) = 1.

Method Calls or steps Time Extra space
Naive recursion 25 calls O(phi^n), about O(1.618^n) O(n) call stack
Memoisation (top-down cache) 11 calls, counting cache hits O(n) O(n) cache plus stack
Tabulation (bottom-up array) 5 additions: 0, 1, 1, 2, 3, 5, 8 O(n) O(n), or O(1) with two variables

The naive call count follows C(n) = C(n - 1) + C(n - 2) + 1, which works out to 2 fib(n + 1) - 1. For n = 6 that is 2 x 13 - 1 = 25. The memoised version makes 2n - 1 = 11 calls, because every value after the first two is computed once and looked up once.

0/1 knapsack

In the 0/1 knapsack problem each item is either taken whole or left. The recurrence is K[i][w] = max(K[i - 1][w], v_i + K[i - 1][w - w_i]), with the second option only when w_i <= w.

Worked example. Capacity 8. Items (value, weight): 1 = (15, 2), 2 = (10, 3), 3 = (30, 5), 4 = (25, 4).

Items used \ capacity 0 1 2 3 4 5 6 7 8
none 0 0 0 0 0 0 0 0 0
1 0 0 15 15 15 15 15 15 15
1-2 0 0 15 15 15 25 25 25 25
1-3 0 0 15 15 15 30 30 45 45
1-4 0 0 15 15 25 30 40 45 45

Answer: 45. Traceback: the final cell 45 equals the cell above it, so item 4 is skipped. In row 1-3, 45 differs from 25 above, so item 3 is taken and capacity drops to 8 - 5 = 3. Row 1-2 at capacity 3 is 15, the same as the row above, so item 2 is skipped; item 1 is taken. The best load is items 1 and 3, weight 7.

Exam angle: the table costs O(nW), which is pseudo-polynomial: polynomial in the value of W, not in the number of bits needed to write it.

Longest common subsequence

A subsequence keeps characters in order but may skip some. LCS(i, j) = LCS(i - 1, j - 1) + 1 when the characters match, otherwise max(LCS(i - 1, j), LCS(i, j - 1)).

Worked example. LCS of AGCAT (rows) and GACTA (columns).

- G A C T A
- 0 0 0 0 0 0
A 0 0 1 1 1 1
G 0 1 1 1 1 1
C 0 1 1 2 2 2
A 0 1 2 2 2 3
T 0 1 2 2 3 3

Answer: length 3. This pair has five different LCSs: ACA, ACT, GAT, GCA and GCT. A traceback that moves up on ties returns ACA; one that moves left on ties can return a different string. That is why exam questions usually ask for the length, or offer only one valid string among the options.

Exam angle: with insertions and deletions only, the edit distance between two strings is m + n - 2 x LCS. Here 5 + 5 - 6 = 4.

LCS table for AGCAT and GACTA: the bottom-right cell is 3, and a traceback that moves up on ties follows the highlighted cells to the subsequence ACA.
The page's LCS example. Diagonal (green) cells are matches that add a character; the traceback moves up on ties.

Coin change: fewest coins

min_coins(a) = 1 + min over coins c <= a of min_coins(a - c), with min_coins(0) = 0.

Worked example. Coins 1, 4 and 6; amount 9.

Amount 0 1 2 3 4 5 6 7 8 9
Fewest coins 0 1 2 3 1 2 1 2 2 3

Answer: 3 coins (4 + 4 + 1). Greedy takes 6 first and then needs 1 + 1 + 1, which is 4 coins. Greedy is safe for coin systems like 1, 5, 10, 25 but not in general; DP always is.

Coin change: counting the ways

ways[0] = 1; for each coin c (outer loop), for each amount a from c upward, ways[a] += ways[a - c].

Worked example. Coins 2, 3 and 5; amount 10.

After coin 0 1 2 3 4 5 6 7 8 9 10
2 1 0 1 0 1 0 1 0 1 0 1
3 1 0 1 1 1 1 2 1 2 2 2
5 1 0 1 1 1 2 2 2 3 3 4

Answer: 4 combinations: 5+5, 2+3+5, 2+2+3+3 and 2+2+2+2+2. If you swap the loops (amounts outside, coins inside), you count ordered sequences instead and get 14, because 2+3+5 and 5+3+2 are then different.

Speed tricks

Common mistakes

Practice set

These are different from the 20 quiz questions below.

  1. How many calls does naive recursive fib(7) make?
  2. 0/1 knapsack, capacity 5; items (value, weight): (3, 2), (4, 3), (5, 4), (6, 5). What is the best value?
  3. Which items give that value?
  4. What is the LCS length of ABCDE and ACEBD?
  5. Give every LCS of ABCDE and ACEBD.
  6. Coins 1, 7 and 10: what are the DP and greedy coin counts for 14?
  7. Coins 3, 5 and 7: how many combinations make 15?
  8. You may climb 1, 2 or 3 steps at a time. In how many ways can you climb 6 steps?
Show answers
  1. 41, which is 2 x fib(8) - 1 = 2 x 21 - 1.
  2. 7.
  3. Items 1 and 2: weight 2 + 3 = 5, value 3 + 4 = 7.
  4. 3.
  5. ABD, ACD and ACE.
  6. DP 2 (7 + 7); greedy 5 (10 + 1 + 1 + 1 + 1).
  7. 3: 5+5+5, 3+5+7 and 3+3+3+3+3.
  8. 24, from ways(n) = ways(n - 1) + ways(n - 2) + ways(n - 3) with ways(0) = 1.

Practise: 20 questions

Interactive quiz loading. The first 10 questions are shown below.

  1. In bottom-up dynamic programming (tabulation), in what order is the table filled?

    1. From the smallest subproblems up to the original problem
    2. From the original problem down to the base cases
    3. In random order, as long as every cell is filled once
    4. Only the cells the recursion actually reaches, on demand
    Show answer

    Answer: A. From the smallest subproblems up to the original problem

    Tabulation starts from the base cases and fills each cell only after the cells it depends on are ready, ending at the cell for the full problem. Computing only the cells that are reached, on demand, describes top-down memoisation.

  2. In the standard 0/1 knapsack table, what does the cell K[i][w] store?

    1. The weight of item i when the capacity is w
    2. The best total value using only the first i items with capacity w
    3. Whether item i fits in capacity w
    4. The number of items chosen from the first i items
    Show answer

    Answer: B. The best total value using only the first i items with capacity w

    K[i][w] = max(K[i - 1][w], K[i - 1][w - w_i] + v_i), the better of skipping or taking item i. The answer to the whole problem is the bottom-right cell K[n][W].

  3. What is the running time of the standard table-based LCS algorithm for strings of lengths m and n?

    1. O(m + n)
    2. O(2^(m + n))
    3. O(mn)
    4. O(mn log(mn))
    Show answer

    Answer: C. O(mn)

    The table has (m + 1)(n + 1) cells and each is filled in O(1) from its left, upper and diagonal neighbours, so the time is O(mn). Brute force over all subsequences of one string would be exponential.

  4. To count the ways to make an amount from coins where order does NOT matter, the loop over coins is placed OUTSIDE the loop over amounts. Why?

    1. So the algorithm runs faster than O(n x amount)
    2. So that larger coins are always used first, like the greedy method
    3. So that each coin can be used only once
    4. So each combination is built in one fixed coin order and counted once
    Show answer

    Answer: D. So each combination is built in one fixed coin order and counted once

    With coins in the outer loop, a combination such as {1, 2} can only be formed by adding 1s before 2s, so it is counted once. Swapping the loops counts 1 + 2 and 2 + 1 separately, which gives ordered sequences (permutations).

  5. A bottom-up Fibonacci keeps only the last two values in two variables instead of a whole array. What are its time and extra space?

    1. O(n) time and O(n) space
    2. O(2^n) time and O(1) space
    3. O(n) time and O(1) space
    4. O(log n) time and O(1) space
    Show answer

    Answer: C. O(n) time and O(1) space

    Each new value needs only the previous two, so the loop runs n times and stores a constant number of variables. The full array version also takes O(n) time but O(n) space.

  6. Why does memoisation turn the naive recursive Fibonacci from exponential time into linear time?

    1. Each fib(k) is computed once and then reused from the cache
    2. It replaces recursion with a faster loop
    3. It uses a closed-form formula instead of recursion
    4. It skips every second recursive call
    Show answer

    Answer: A. Each fib(k) is computed once and then reused from the cache

    Naive recursion recomputes the same subproblems again and again (overlapping subproblems). A cache stores fib(k) the first time it is computed, so there are only n + 1 distinct values to work out.

  7. A naive recursive fib(n) returns n when n < 2 and otherwise calls fib(n - 1) + fib(n - 2). How many calls in total, including the first, does fib(8) make?

    1. 21
    2. 15
    3. 41
    4. 67
    Show answer

    Answer: D. 67

    The call count C(n) satisfies C(n) = C(n - 1) + C(n - 2) + 1 with C(0) = C(1) = 1, which gives C(n) = 2 fib(n + 1) - 1. For n = 8, fib(9) = 34, so C(8) = 67. A memoised version makes only 15 calls.

  8. 0/1 knapsack: capacity 6; items (value, weight) are (6, 1), (10, 2), (12, 3), (13, 4). What is the maximum total value?

    1. 29
    2. 28
    3. 23
    4. 36
    Show answer

    Answer: B. 28

    Taking the first three items uses weight 1 + 2 + 3 = 6 and gives 6 + 10 + 12 = 28. The next best options are 10 + 13 = 23 (weight 6) or 6 + 13 = 19. 36 would need six copies of the first item, which 0/1 knapsack forbids.

  9. For the same knapsack (capacity 6; items (6, 1), (10, 2), (12, 3), (13, 4)), what is the table cell K[3][4], the best value using only the first three items with capacity 4?

    1. 16
    2. 18
    3. 22
    4. 13
    Show answer

    Answer: B. 18

    With items 1 to 3 and capacity 4, the choices are item 1 + item 3 (weight 4, value 18) or item 1 + item 2 (weight 3, value 16). By the formula, K[3][4] = max(K[2][4], K[2][1] + 12) = max(16, 6 + 12) = 18.

  10. What is the length of the longest common subsequence of STONE and LONGEST?

    1. 2
    2. 4
    3. 5
    4. 3
    Show answer

    Answer: D. 3

    ONE appears in order in both strings (S-T-O-N-E and L-O-N-G-E-S-T), and no 4-letter common subsequence exists. The DP table's bottom-right cell is 3.

FAQs

How do I recognise a DP problem in an MCQ?

Look for an optimisation or counting question ("maximum value", "minimum coins", "number of ways", "longest") where a brute-force answer would try every combination. If the problem breaks into smaller versions of itself that repeat, and the best answer is built from the best smaller answers, DP applies.

Memoisation or tabulation: which should I write?

They have the same time complexity. Memoisation is easier to write from a recursive formula and computes only the states it needs. Tabulation avoids recursion-depth limits and makes space savings, such as keeping two rows, easy to see. Exams usually show tabulation because the table can be printed and traced.

Is Dijkstra's algorithm dynamic programming?

It is normally classed as greedy, but it relies on the same optimal-substructure property as DP. Bellman-Ford and Floyd-Warshall are the shortest-path algorithms usually presented as DP. See graph traversal traces for Dijkstra worked step by step.

Why is 0/1 knapsack hard if the DP is O(nW)?

W is a number, and writing it takes only about log W bits. So O(nW) is exponential in the input size when W is large, which is why 0/1 knapsack is NP-hard even though small cases are quick to solve. Fractional knapsack, by contrast, is solved exactly by greedy.

Next step

Practise on the 20 DP MCQs below, then compare DP with greedy and divide and conquer in Algorithm Design Techniques MCQs and check your complexity reasoning in Big-O practice. Return to the DSA MCQ hub or sit an NSCT mock test.