322. Coin Change
Asked at Google, Meta, Amazon, Microsoft and 4 more
Difficulty
Medium
Time
O(amount × coins)
Space
O(amount)
Pattern
Dynamic Programming
Companies
8
Problem
You are given coin denominations and a target amount. Return the fewest coins that add up to that amount, or -1 if no combination of the coins can reach it.
You have an unlimited supply of every denomination, and you can use the same coin as many times as you like.
Fewest coins, not any coins
Constraints
- ●
1 <= coins.length <= 12 - ●
1 <= coins[i] <= 2^31 - 1 - ●
0 <= amount <= 10^4
Examples
coins = [1,2,5], amount = 113coins = [2], amount = 3-1coins = [1], amount = 00Asked At
8 companies| Company | Difficulty | |
|---|---|---|
| Medium | View all Google questions → | |
| Meta | Medium | View all Meta questions → |
| Amazon | Medium | View all Amazon questions → |
| Microsoft | Medium | View all Microsoft questions → |
| Apple | Medium | View all Apple questions → |
| Uber | Medium | View all Uber questions → |
| Walmart | Medium | View all Walmart questions → |
| Medium | View all Pinterest questions → |
How to Think About It
This is an unbounded knapsack variant. You can use each coin unlimited times. dp[i] = minimum coins to make amount i.
The recurrence: dp[i] = min(dp[i], dp[i - coin] + 1) for each coin where coin <= i. You're checking "if I use this coin, how many more do I need?"
Base case: dp[0] = 0 (zero coins needed for amount 0). Fill dp[1] through dp[amount] left to right.
Why bottom-up works: to compute dp[i], you need dp[i-coin] which is a smaller amount. By filling left to right, smaller amounts are always computed first.
Edge cases: amount=0 (return 0), no valid combination (return -1), single coin denomination.
Optimal Approach
Step 1: Create dp array of size amount+1, fill with infinity. dp[0] = 0.
Step 2: For i from 1 to amount:
For each coin where coin <= i:
dp[i] = min(dp[i], dp[i - coin] + 1)
Step 3: Return dp[amount] if not infinity, else -1.
The logic: if you use a coin of value "coin", you need 1 coin plus however many coins were needed for amount "i - coin".
Time: O(amount × number of coins). Space: O(amount).
Build the table from 0 upward
What Trips People Up in Real Interviews
Confusing "minimum coins" with "counting ways." This problem asks for the fewest coins, not the number of ways to make the amount.
Not handling the case where no combination works. Return -1, not 0 or null.
Forgetting to initialize dp[0] = 0. Zero coins are needed to make amount 0.
Using BFS instead of DP. BFS works (each level is one more coin) but DP is simpler and more efficient for this problem.
Filling the dp array in the wrong order. You must fill left to right (dp[0] to dp[amount]) because each dp[i] depends on smaller amounts (dp[i - coin]). Filling right to left gives wrong results.
Solution Code
O(amount × coins) timedef coinChange(coins, amount):
dp = [float('inf')] * (amount + 1)
dp[0] = 0
for i in range(1, amount + 1):
for coin in coins:
if coin <= i:
dp[i] = min(dp[i], dp[i - coin] + 1)
return dp[amount] if dp[amount] != float('inf') else -1Frequently Asked Questions
What is the Coin Change problem?
You are given coins of different denominations and a total amount. Write a function to compute the fewest number of coins needed to make up that amount. If it is not possible, return -1.
How do you solve Coin Change?
The optimal approach runs in O(amount × coins) time and O(amount) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Coin Change?
Coin Change is asked at Google, Meta, Amazon, Microsoft, Apple, Uber, Walmart, Pinterest. It is a medium difficulty problem.
What are common mistakes on Coin Change?
- Confusing "minimum coins" with "counting ways." This problem asks for the fewest coins, not the number of ways to make the amount.
- Not handling the case where no combination works. Return -1, not 0 or `null`.
- Forgetting to initialize `dp[0]` = 0. Zero coins are needed to make amount 0.
- Using BFS instead of DP. BFS works (each level is one more coin) but DP is simpler and more efficient for this problem.
- Filling the dp array in the wrong order. You must fill left to right (`dp[0]` to `dp[amount]`) because each `dp[i]` depends on smaller amounts (`dp[i - coin]`). Filling right to left gives wrong results.