Fewest Coins for an Amount with Unlimited or Single-Use Coins
Company: Oracle
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: Return the fewest coins that add up exactly to an amount, first when every coin value can be reused without limit and then when each listed coin can be used at most once. An onsite coding problem that tests the difference between unlimited and single-use selection and the handling of amounts that cannot be formed.
Read the full Oracle Software Engineer interview experience this question came from
Constraints
- 1 <= len(coins) <= 100
- 1 <= coins[i] <= 10^4
- 0 <= amount <= 10^4
- unlimited is True or False
Examples
Input: ([7], 0, True)
Expected Output: 0
Explanation: amount = 0 needs no coins in the unlimited form.
Input: ([1, 2, 3], 0, False)
Expected Output: 0
Explanation: amount = 0 needs no coins in the at-most-once form.
Hints
- Example 1 shows that always taking the largest coin that fits can use more coins than necessary, so the choice must account for more than the biggest value.
- amount is at most 10^4, so it is feasible to reason about every total from 0 up to amount and how few coins reach each one.
- The two forms differ only in whether a coin is still available after it is used: in the at-most-once form make sure one list element is never counted twice, while equal values at different positions remain separate coins.