Coin Change
Problem
You have an unlimited supply of coins in each of the denominations listed in coins, and a target amount. Return the smallest number of coins whose values sum exactly to amount. If no combination of the given coins can produce amount, return -1.
coinsholds up to about a dozen distinct positive denominations, each of which can be a large positive integer (large enough that you should not index anything by coin value).amountis a non-negative target up to roughly ten thousand.
Examples
coins = [1, 4, 6], amount = 13→3, using6 + 6 + 1.coins = [3], amount = 5→-1. Only multiples of 3 are reachable with a single 3-coin.coins = [1, 7, 10], amount = 14→2(7 + 7). Greedy would take a 10 first and end at10 + 1 + 1 + 1 + 1 = 5coins, which is wrong.
Hints
- Try the greedy approach of always taking the largest coin on
coins = [1, 7, 10], amount = 14. What goes wrong? - If you had the optimal answer for every amount smaller than
amount, how would you compute it foramountby considering only the last coin used? - This is unbounded knapsack:
fewest[a] = 1 + min(fewest[a - c] for c in coins if c <= a), filled from 0 up toamount.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.