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?
- A. Greedy is optimal: 2 coins
- B. Greedy uses 5 coins (10 + 1 + 1 + 1 + 1); optimal is 2 coins (7 + 7)
- C. Greedy uses 3 coins; optimal is 2
- 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
- Problem: Split a string into as many parts as possible so each letter appears in at most one part; return the part sizes. python def…
- Problem: Jobs have deadlines and profits; each takes one unit of time. Schedule to maximise profit (greedy by profit, latest free slot)…
- Problem: Given n ≤ 10⁵ intervals, find the minimum number of points such that every interval contains at least one point. Which greedy…
- Problem: Given n balloons as horizontal intervals, find the minimum number of vertical arrows that burst all of them (a balloon bursts if…
- Problem: Insert a new interval into a sorted list of non-overlapping intervals, merging if necessary. python def insert(iv, new): out, i…
- Problem: n ≤ 10⁵ trains arrive and depart at given minutes. Find the minimum number of platforms. Which approach is O(n log n) and correct?