Find the fewest coins that make an amount with bottom-up dynamic programming in O(amount × coins), and see why greedy fails. FAANG DP staple, run live.
The problem
Return the fewest coins from coins (unlimited supply of each) that add up to amount, or -1 if it can't be made.
Examples
Example 1
Input
coin_change([1, 2, 5], 11)
Expected output
3
Example 2
Input
coin_change([2], 3)
Expected output
-1
+ 3 hidden tests on Submit — greedy would say 3.
Edge cases to ask about
- amount = 0
- Impossible amount
- Greedy counter-example
Hints
0/3How an interviewer scores this
0/9Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.
Complexity Lab
What does this cost as n grows?
Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.
Pick both to reveal the answer.
Measure it
Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.
From brute force to optimal
The progression an interviewer wants to hear, one step at a time.
| Approach | Time | Space | Idea |
|---|---|---|---|
| Greedy (largest coin first) | O(amount) | O(1) | WRONG for coins like [1, 3, 4] and amount 6. |
| Recursion over every choice | O(coinsᵃᵐᵒᵘⁿᵗ) | O(amount) | |
| bestBottom-up DP | O(amount · k) | O(amount) | dp[v] = 1 + min(dp[v − c]). |
Walkthrough of the optimal approach (try it yourself first)
Greedy fails ([1, 3, 4], amount 6: greedy gives 4+1+1, the best is 3+3). DP: dp[v] is the fewest coins for value v; the last coin used is some c, so dp[v] = 1 + min(dp[v − c]). Fill from 0 up; amount + 1 works as "infinity" because no answer can need more coins than that.
Name the state (dp[v]), the transition and the base case (dp[0] = 0) — that's what an interviewer grades in any DP answer.
Complexity: O(amount · k) time, O(amount) space. For each of the amount + 1 values, every one of the k coins is tried once.
Reveal the reference solution
def coin_change(coins, amount): INF = amount + 1 dp = [0] + [INF] * amount for value in range(1, amount + 1): for c in coins: if c <= value and dp[value - c] + 1 < dp[value]: dp[value] = dp[value - c] + 1 return dp[amount] if dp[amount] != INF else -1
Follow-ups interviewers ask
- Count the NUMBER of ways (Coin Change II).
- Return the coins used.
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Coin Change: Minimum Number of Coins in Python?
The optimal solution runs in O(amount · k) time and O(amount) auxiliary space. For each of the amount + 1 values, every one of the k coins is tried once.
What is the brute-force approach, and how do you optimise it?
Greedy (largest coin first): O(amount) time, O(1) space. WRONG for coins like [1, 3, 4] and amount 6. Recursion over every choice: O(coinsᵃᵐᵒᵘⁿᵗ) time, O(amount) space. Bottom-up DP: O(amount · k) time, O(amount) space. dp[v] = 1 + min(dp[v − c]).
What follow-up questions do interviewers ask about Coin Change: Minimum Number of Coins?
Count the NUMBER of ways (Coin Change II). Return the coins used.
