Data Structures & AlgorithmsHardAccenture · Infosys OA
Problem: Which of these coin systems is a classic counterexample where the greedy 'always take the largest coin' algorithm fails to find the minimum number of coins?
- A. No coin system can break greedy
- B. Coins {1, 5, 10, 25} (standard currency), any amount
- C. Coins {1, 3, 4}, amount 6: greedy gives 4+1+1 (3 coins) but the optimum is 3+3 (2 coins)
- D. Coins {1, 2, 5}, any amount
Correct answer: C. Coins {1, 3, 4}, amount 6: greedy gives 4+1+1 (3 coins) but the optimum is 3+3 (2 coins)
With coins {1,3,4}, greedy picks the largest coin not exceeding the remainder first: for 6 that's 4, then 1, then 1 (3 coins), but 3+3 needs only 2; this shows greedy is only safe for canonical coin systems, and DP is needed in general.
More Coding Interview Problems questions
- Problem: Huffman coding builds an optimal prefix-free binary code for a set of symbol frequencies. Which greedy step does it repeat?
- Problem: What is the time complexity of computing the longest increasing subsequence using patience-sorting with binary search, versus the…
- Problem: The longest common subsequence (LCS) of two strings of length n and m is typically solved with which approach and complexity?
- Problem: Subset-sum (does any subset of n integers sum exactly to a target T?) has a DP solution running in O(nT). Why is this not…
- Problem: What is the average-case and worst-case time complexity of the quickselect algorithm for finding the k-th smallest element?
- Problem: Which of these sorting algorithms is guaranteed O(n log n) in the worst case AND is stable (preserves the relative order of equal…