Coin Change II
Problem
You are given a list of distinct coin denominations coins and a target amount. Each denomination may be used any number of times. Count how many different combinations of coins sum exactly to amount. Two combinations are the same if they use the same number of each denomination; order does not matter, so 2 + 3 and 3 + 2 are one combination. Return 0 if the amount cannot be formed.
coinsholds up to a few hundred distinct denominations.- Each denomination and
amountfit comfortably in a 32-bit range, and so does the answer.
Examples
coins = [1, 3, 4], amount = 7→5:4+3,4+1+1+1,3+3+1,3+1+1+1+1,1+1+1+1+1+1+1.coins = [2], amount = 3→0. Odd amounts are unreachable with only 2s.coins = [9], amount = 9→1. One coin, one way.
Hints
- Write a recursive function that counts ways. If it treats
2+3and3+2as different, what extra decision could you fix to avoid that? - Process denominations one at a time. For each coin, decide how the number of ways to reach every amount changes once that coin becomes available.
- Unbounded knapsack with the coin loop on the outside:
ways[a] += ways[a - c]for each coinc, forafromcup 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.