DSA interview question
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.
int minCoins(vector<int>& coins, int X);
coins | X | Output | Explanation |
|---|---|---|---|
[1,2,5] | 11 | 3 | 5 + 5 + 1 = 11, using 3 coins |
[2] | 3 | -1 | impossible — can't make an odd amount using only 2s |
[1] | 0 | 0 | zero amount needs zero coins |
[1] | 5 | 5 | five 1-coins |
[] | 5 | -1 | no coins available, positive amount unreachable |
[] | 0 | 0 | zero amount always achievable with zero coins, even with no coins available |
[3,7,10] | 14 | 2 | 7 + 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.)
X = 0 return? (Standard convention: 0 — zero coins needed.)X and number of coin denominations? (Signals whether O(coins × X) DP is acceptable.)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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in