Dynamic Programming Questions with Answers

87 practice questions on Dynamic Programming (Data Structures & Algorithms) with answers and step-by-step solutions. Try 10 samples below, then practise all 87 free in the AptiNest app.

Sample questions

Question 1Easy

Memoisation is:

  1. A greedy heuristic
  2. Top-down recursion that caches results of subproblems
  3. Bottom-up table filling
  4. Sorting the input before recursion

Answer: B. Top-down recursion that caches results of subproblems

Explanation

Each subproblem is computed once and looked up afterwards; tabulation is the bottom-up alternative.

Step-by-step solution

  1. Write the natural recursive solution.
  2. Before computing a subproblem, check a cache; after computing, store it.
  3. Each distinct subproblem is now solved once.

Why the other options are wrong

  • A. Memoisation is exact caching, not a heuristic choice.
  • C. Bottom-up table filling is tabulation, the other style of DP.

Tip: Memoisation = top-down + cache; tabulation = bottom-up + table.

Question 2Easy

A robot moves only right or down on a 4 x 5 grid (4 rows, 5 columns), from the top-left to the bottom-right cell. How many distinct paths are there?

  1. 12
  2. 35
  3. 70
  4. 20

Answer: B. 35

Explanation

It makes 3 down and 4 right moves in some order: C(7, 3) = 35.

Step-by-step solution

  1. From top-left to bottom-right: 3 down moves and 4 right moves.
  2. A path is an arrangement of these 7 moves.
  3. Number = C(7, 3) = 35.

Why the other options are wrong

  • A. That is 4 x 3, not a path count.
  • C. C(8, 4) = 70 is for 5 x 5 grid moves (4 down, 4 right).
  • D. That is 4 x 5, the number of cells.

Tip: m x n grid: C(m + n - 2, m - 1).

Question 3Easy

Houses have money [6, 1, 2, 7, 3, 9, 4]. You cannot rob two adjacent houses. What is the maximum amount you can rob?

  1. 16
  2. 17
  3. 23
  4. 22

Answer: D. 22

Explanation

dp[i] = max(dp[i-1], dp[i-2] + a[i]); the final value is 22.

Step-by-step solution

  1. dp: 6, 6, 8, 13, 13, 22, 22.
  2. Rob 6, 7, 9: positions 0, 3, 5.
  3. 6 + 7 + 9 = 22.

Why the other options are wrong

  • A. 16 is reachable (7 + 9) but it is not the maximum.
  • B. Not the maximum set.
  • C. No non-adjacent selection sums to 23.

Tip: Skipping is free; adjacency is the only rule.

Question 4Easy

In how many ways can a 2 x 5 board be tiled with 1 x 2 dominoes?

  1. 16
  2. 9
  3. 8
  4. 5

Answer: C. 8

Explanation

The last column is either one vertical domino (T(n-1)) or two horizontal ones (T(n-2)), so T(n) is Fibonacci-like: T(5) = 8.

Step-by-step solution

  1. T(1) = 1, T(2) = 2.
  2. T(n) = T(n-1) + T(n-2).
  3. T(3) = 3, T(4) = 5, T(5) = 8.

Why the other options are wrong

  • A. 2^4 treats each column as independent.
  • B. Arithmetic slip.
  • D. That is T(4).

Tip: 2 x n domino tilings are Fibonacci numbers.

Question 5Medium

What is the AREA of the largest square containing only 1s in this matrix?

0111
1111
1111
0110
  1. 3
  2. 12
  3. 16
  4. 9

Answer: D. 9

Explanation

dp[i][j] = 1 + min(top, left, top-left) gives the largest side 3, so the area is 9.

Step-by-step solution

  1. Fill dp: row 1 is 1, 1, 2, 2; row 2 is 1, 2, 2, 3.
  2. Largest side = 3 (rows 0-2, columns 1-3).
  3. Area = 9.

Why the other options are wrong

  • A. That is the side, not the area.
  • B. 12 would be a 3 x 4 rectangle, not a square.
  • C. The full 4 x 4 contains 0s.

Tip: Area = side squared.

Question 6Medium

What is the length of the longest strictly increasing subsequence of [3, 10, 2, 1, 20, 4, 6, 7]?

  1. 3
  2. 5
  3. 4
  4. 6

Answer: C. 4

Explanation

With LIS[i] = 1 + max LIS[j] over j < i with a[j] < a[i], the values are [1, 2, 1, 1, 3, 2, 3, 4]; the maximum is 4.

Step-by-step solution

  1. LIS ending at each index: [1, 2, 1, 1, 3, 2, 3, 4].
  2. Index 7 (value 7) gets 4 via 3 -> 4 -> 6 -> 7.
  3. Maximum = 4.

Why the other options are wrong

  • A. Stops at 3, 10, 20 and misses 4, 6, 7.
  • B. No increasing chain of 5 exists.
  • D. Too long; the array has only 8 elements and several drops.

Tip: Example LIS: 3, 4, 6, 7 (or 2, 4, 6, 7).

Question 7Medium

Moving only right or down, what is the minimum path sum from the top-left to the bottom-right cell?

5 1 2 4
3 9 1 2
4 1 7 1
2 2 3 6
  1. 20
  2. 21
  3. 18
  4. 19

Answer: C. 18

Explanation

dp[i][j] = grid[i][j] + min(top, left); the bottom-right value is 18.

Step-by-step solution

  1. Row 0: 5, 6, 8, 12.
  2. Row 1: 8, 15, 9, 11.
  3. Row 2: 12, 13, 16, 12.
  4. Row 3: 14, 15, 18, 18.
  5. Path 5 -> 1 -> 2 -> 1 -> 2 -> 1 -> 6 = 18.

Why the other options are wrong

  • A. A route through a larger cell such as 7 or 9.
  • B. Not the minimum route.
  • D. Off by one; recheck the last column (12 + 6 = 18).

Tip: Fill the first row and column first; they have only one way in.

Question 8Medium

In how many ways can "catsanddog" be split into a sequence of words from the dictionary {and, cat, cats, dog, sand}?

  1. 4
  2. 1
  3. 3
  4. 2

Answer: D. 2

Explanation

dp[i] = sum of dp[j] for every dictionary word s[j:i]; dp[10] = 2.

Step-by-step solution

  1. "cats" + "and" + "dog".
  2. "cat" + "sand" + "dog".
  3. No other split works: 2 ways.

Why the other options are wrong

  • A. Over-counts.
  • B. Misses one of cat/cats.
  • C. Over-counts by one.

Tip: dp[0] = 1; add dp[j] whenever s[j:i] is a word.

Question 9Hard

What does this code print?

def f(dice, faces, target):
    dp = [1] + [0] * target
    for _ in range(dice):
        dp = [sum(dp[t - k] for k in range(1, faces + 1) if t - k >= 0) for t in range(target + 1)]
    return dp[target]

print(f(2, 6, 7), f(3, 6, 10))
  1. 5 27
  2. 6 25
  3. 6 27
  4. 6 21

Answer: C. 6 27

Explanation

dp counts ordered dice outcomes: 2 dice make 7 in 6 ways, and 3 dice make 10 in 27 ways.

Step-by-step solution

  1. dp[t] = number of ordered ways to reach sum t with the dice so far.
  2. Two dice summing to 7: (1,6), (2,5), (3,4), (4,3), (5,2), (6,1) = 6.
  3. Three dice summing to 10: the DP gives 27.
  4. Output 6 27.

Why the other options are wrong

  • A. Misses one ordered pair for 7.
  • B. Miscounts the three-dice total.
  • D. Counts 3 dice summing to 8 or 9 instead of 10.

Tip: Two dice: 7 is the most likely sum, with 6 ways out of 36.

Question 10Hard

Weighted interval scheduling: jobs (start, end, profit) = (1, 3, 50), (2, 4, 10), (3, 5, 40), (3, 6, 70). A job may start when another ends. What is the maximum total profit of non-overlapping jobs?

  1. 70
  2. 120
  3. 90
  4. 160

Answer: B. 120

Explanation

Sort by end time; dp[i] = max(dp[i-1], profit[i] + dp[last compatible job]). The best is (1, 3, 50) + (3, 6, 70) = 120.

Step-by-step solution

  1. Sort by end: (1,3,50), (2,4,10), (3,5,40), (3,6,70).
  2. (3,6,70) can follow (1,3,50) since it starts when that one ends: 120.
  3. (3,5,40) after (1,3,50) gives only 90; (2,4,10) overlaps both others.
  4. Maximum = 120.

Why the other options are wrong

  • A. 70 is just the single best job; it can be combined with (1,3,50).
  • C. 50 + 40 = 90 is valid but 50 + 70 is better.
  • D. 160 adds 50 + 40 + 70, but (3,5) and (3,6) overlap.

Tip: Sort by end time; dp[i] = max(dp[i-1], profit[i] + dp[last compatible]).

Practise all 87 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Data Structures & Algorithms topics · All topics