Question 1Easy
- A greedy heuristic
- Top-down recursion that caches results of subproblems
- Bottom-up table filling
- 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
- Write the natural recursive solution.
- Before computing a subproblem, check a cache; after computing, store it.
- 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?
- 12
- 35
- 70
- 20
Answer: B. 35
Explanation
It makes 3 down and 4 right moves in some order: C(7, 3) = 35.
Step-by-step solution
- From top-left to bottom-right: 3 down moves and 4 right moves.
- A path is an arrangement of these 7 moves.
- 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?
- 16
- 17
- 23
- 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
- dp: 6, 6, 8, 13, 13, 22, 22.
- Rob 6, 7, 9: positions 0, 3, 5.
- 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?
- 16
- 9
- 8
- 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
- T(1) = 1,
T(2) = 2.
- T(n) = T(n-1) + T(n-2).
- 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
- 3
- 12
- 16
- 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
- Fill dp: row 1 is 1, 1, 2, 2; row 2 is 1, 2, 2, 3.
- Largest side = 3 (rows 0-2, columns 1-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]?
- 3
- 5
- 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
- LIS ending at each index: [1, 2, 1, 1, 3, 2, 3, 4].
- Index 7 (value 7) gets 4 via 3 -> 4 -> 6 -> 7.
- 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
- 20
- 21
- 18
- 19
Answer: C. 18
Explanation
dp[i][j] = grid[i][j] + min(top, left); the bottom-right value is 18.
Step-by-step solution
- Row 0: 5, 6, 8, 12.
- Row 1: 8, 15, 9, 11.
- Row 2: 12, 13, 16, 12.
- Row 3: 14, 15, 18, 18.
- 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}?
- 4
- 1
- 3
- 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
- "cats" + "and" + "dog".
- "cat" + "sand" + "dog".
- 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))
- 5 27
- 6 25
- 6 27
- 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
- dp[t] = number of ordered ways to reach sum t with the dice so far.
- Two dice summing to 7: (1,6), (2,5), (3,4), (4,3), (5,2), (6,1) = 6.
- Three dice summing to 10: the DP gives 27.
- 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?
- 70
- 120
- 90
- 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
- Sort by end: (1,3,50), (2,4,10), (3,5,40), (3,6,70).
- (3,6,70) can follow (1,3,50) since it starts when that one ends: 120.
- (3,5,40) after (1,3,50) gives only 90; (2,4,10) overlaps both others.
- 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]).