Dynamic Programming Problems: Worked Tables for NSCT
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
- 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.
- Write the recurrence, usually a choice: take or skip, match or skip, use this coin or not.
- Fix the base cases: row 0 and column 0 are usually 0, except ways-counting, where ways[0] = 1.
- Choose the fill order so every cell's dependencies are filled first: left to right and top to bottom for 2-D tables.
- 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.

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
- Fibonacci call count = 2 fib(n + 1) - 1. Memorise fib up to 12: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144.
- Knapsack shortcut: for four or five items, list the combinations that fit and add their values; it is often faster than the full table.
- LCS check: the answer can never exceed the shorter string's length, and any option string must appear in order in both strings.
- Greedy coin test: try amounts just above a big coin (here 9 = 6 + 3 versus 4 + 4 + 1). That is where greedy usually breaks.
Common mistakes
- Starting ways-counting tables with ways[0] = 0. There is exactly one way to make 0: use no coins.
- Looping capacity upward in a 1-D 0/1 knapsack, which silently lets items be reused. Loop downward.
- Treating "subsequence" as "substring". Substrings must be contiguous; subsequences need not be.
- Taking an item in the knapsack recurrence without checking w_i <= w first, which reads a column that does not exist.
- Assuming memoisation saves stack space. It saves time; recursion depth is still O(n).
Practice set
These are different from the 20 quiz questions below.
- How many calls does naive recursive fib(7) make?
- 0/1 knapsack, capacity 5; items (value, weight): (3, 2), (4, 3), (5, 4), (6, 5). What is the best value?
- Which items give that value?
- What is the LCS length of ABCDE and ACEBD?
- Give every LCS of ABCDE and ACEBD.
- Coins 1, 7 and 10: what are the DP and greedy coin counts for 14?
- Coins 3, 5 and 7: how many combinations make 15?
- You may climb 1, 2 or 3 steps at a time. In how many ways can you climb 6 steps?
Show answers
- 41, which is 2 x fib(8) - 1 = 2 x 21 - 1.
- 7.
- Items 1 and 2: weight 2 + 3 = 5, value 3 + 4 = 7.
- 3.
- ABD, ACD and ACE.
- DP 2 (7 + 7); greedy 5 (10 + 1 + 1 + 1 + 1).
- 3: 5+5+5, 3+5+7 and 3+3+3+3+3.
- 24, from ways(n) = ways(n - 1) + ways(n - 2) + ways(n - 3) with ways(0) = 1.
Practise: 20 questions
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.