Data Structures & AlgorithmsModerateTCS NQT · Product Company OA

Problem: What is the minimum number of coins from {1, 2, 5} that sum to 11? Run the DP.

Problem: What is the minimum number of coins from {1, 2, 5} that sum to 11? Run the DP.
INF = 10 ** 9
d = [0] + [INF] * 11
for v in range(1, 12):
    d[v] = min(d[v - c] + 1 for c in (1, 2, 5) if v >= c)
print(d[11])
What is printed?
  1. A. 3
  2. B. 4
  3. C. 2
  4. D. 5

Correct answer: A. 3

Two 5s and one 1 give 11 with 3 coins; two coins cannot reach 11 since the largest is 5.

More Coding Interview Problems questions

Exams that ask this topic