Data Structures & AlgorithmsEasyTCS NQT Digital · Product Company SDE

Problem: Coins of denominations {1, 7, 10} are available in unlimited supply. Pay 14 with the fewest coins. What does the greedy 'largest coin first' strategy give, and what is optimal?

  1. A. Greedy is optimal: 2 coins
  2. B. Greedy uses 5 coins (10 + 1 + 1 + 1 + 1); optimal is 2 coins (7 + 7)
  3. C. Greedy uses 3 coins; optimal is 2
  4. D. Greedy uses 5 coins; optimal is 3

Correct answer: B. Greedy uses 5 coins (10 + 1 + 1 + 1 + 1); optimal is 2 coins (7 + 7)

Greedy takes 10 and then four 1s. 7 + 7 = 14 uses two coins. Greedy is only safe for canonical systems such as {1, 2, 5, 10}; in general use the coin-change DP, O(amount × coins).

More Coding Interview Problems questions

Exams that ask this topic