Coin Change
How the interviewer asks it
“Given coin values and an amount, return the fewest coins needed to make that amount, or -1 if it cannot be made. You have unlimited coins of each value.”
11 1 2 5
3
In the practice room your code reads the input from standard input, and Run checks it against tests like this one. Try it now in your browser, in Python or JavaScript.
Typed drills are free and unlimited: the AI scores your answer against the outline below.
What the interviewer is looking for
- You show a counterexample to greedy, such as coins [1, 3, 4] and amount 6.
- A clear recurrence: best[x] = 1 + min(best[x − c]) over every coin c ≤ x.
- Correct base case best[0] = 0 and an "infinity" for unreachable amounts.
- You can explain both top-down memoisation and bottom-up tables.
How to structure your answer
- 01
Clarify
Ask whether the amount can be 0, how large it can be, and whether coin values are distinct.
- 02
Why not greedy
Give a counterexample so the interviewer knows you checked.
- 03
Recurrence
Define best[x] as the fewest coins for amount x, with best[0] = 0.
- 04
Bottom-up
Fill best from 1 to the amount, trying each coin. Return -1 if best[amount] is still infinity.
- 05
Complexity
O(amount × coins) time and O(amount) space.
Best answers from candidates
No candidate answers yet. Type yours, and if it scores 6 or more you can show it here without your name.
Answers are shared by candidates, without names, after a privacy check. Add yours
Common mistakes
- Starting with greedy and defending it after a counterexample.
- Initialising the table with 0 or -1 and then taking a minimum over it.
- Returning infinity (or a huge number) instead of -1.
Follow-up questions to expect
- Now return the number of distinct ways to make the amount. (Loop coins outside amounts so orders are not counted twice.)
- Return the coins used, not just the count.
Now answer it out loud
Max will ask you this exact question, listen to your answer, push on the weak spots with follow-ups like the ones above, and score you when you finish.
Practice with MaxFree: a 5-minute voice round and unlimited typed drills, with scored feedback. No card, and your camera is never recorded.
More coding interview questions
Preparing for this round? Read how to prepare for a coding interview: a four-week DSA plan.