Data Structures & AlgorithmsHardTCS CodeVita · Microsoft SDE OA

Coins of denominations 1, 3 and 4 are available with unlimited supply. For the amount 6, run the DP that finds the minimum number of coins summing exactly to 6.

Coins of denominations 1, 3 and 4 are available with unlimited supply. For the amount 6, run the DP that finds the minimum number of coins summing exactly to 6.
coins=[1,3,4]; A=6; INF=10**9
g=[0]+[INF]*A
for x in range(1,A+1):
    g[x]=min([g[x-c]+1 for c in coins if c<=x]+[INF])
print(g[A])
What is printed?
  1. A. 4
  2. B. 3
  3. C. 2
  4. D. -1

Correct answer: C. 2

Unbounded coin DP gives 3+3, which is 2 coins. Greedy takes 4+1+1 and uses 3 coins, counting combinations instead of coins gives 4, and treating each coin as single-use makes 6 unreachable.

More Coding Interview Problems questions

Exams that ask this topic