PPractick

DSA interview question

Minimum Coins (Coin Change 1)

Hard · Algorithms · Topics: dynamic programming, unbounded knapsack

The problem

You're building the "optimal change dispenser" logic for a vending-machine network. Each machine stocks an unlimited supply of a fixed set of coin denominations, and to minimize mechanical wear (fewer coins dropped = less jamming risk), the machine should dispense change using the fewest coins possible. Given a coins array (unlimited supply of each denomination) and a target amount X, find the minimum number of coins needed to make up that amount. If it's impossible to make the amount with the given denominations, return -1.

Function signature

int minCoins(vector<int>& coins, int X);

Test cases

coinsXOutputExplanation
[1,2,5]1135 + 5 + 1 = 11, using 3 coins
[2]3-1impossible — can't make an odd amount using only 2s
[1]00zero amount needs zero coins
[1]55five 1-coins
[]5-1no coins available, positive amount unreachable
[]00zero amount always achievable with zero coins, even with no coins available
[3,7,10]1427 + 7 = 14, using 2 coins (not 10+3+... which would need more coins)

(Key contrast to flag upfront: this is DIFFERENT from Coin Change 2 — that counted the number of combinations; this minimizes coin count. Same input shape, completely different DP transition.)

How interviewers phrase it

Clarifying questions to ask

  1. Is each coin denomination available in unlimited supply? (Confirm — unbounded knapsack, not 0/1.)
  2. What should be returned if the amount is unreachable? (Standard convention: -1.)
  3. What should X = 0 return? (Standard convention: 0 — zero coins needed.)
  4. Are coin values guaranteed positive? Could there be duplicates or a 0-valued coin in the array? (A 0-valued coin would be degenerate — infinite coins could theoretically be "added" without changing the sum, but wouldn't reduce coin count, so it's usually safe to ignore, but worth flagging.)
  5. What's the expected range of X and number of coin denominations? (Signals whether O(coins × X) DP is acceptable.)

Follow-ups to expect

  1. "How is this different from Coin Change 2 (counting combinations)?"
  2. "Can you reconstruct which coins were actually used, not just the count?"
  3. "Can you optimize space?"
  4. "Why doesn't loop order (coins outer vs. amount outer) matter here, unlike Coin Change 2?"
  5. "What if this needs to run for many different target amounts with the same coin set?"
  6. "Is a greedy approach (always use the largest coin first) valid?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in