DSA interview question
You're building the "checkout settlement" module for a vending-machine network. Each machine accepts a fixed set of coin denominations, and management wants to know, for auditing purposes, how many distinct ways a given target amount could have been paid using those denominations — not the minimum coins, but the total count of distinct combinations (where order doesn't matter — paying with a nickel-then-dime is the same combination as dime-then-nickel). Given an integer array coins (unlimited supply of each denomination) and an integer amount, return the number of combinations that make up that amount.
int change(int amount, vector<int>& coins);
coins | amount | Output | Explanation |
|---|---|---|---|
[1,2,5] | 5 | 4 | {5}, {2,2,1}, {2,1,1,1}, {1,1,1,1,1} |
[2] | 3 | 0 | can't make 3 using only 2s |
[1] | 0 | 1 | exactly one way to make 0: use no coins |
[] | 5 | 0 | no coins available, can't make positive amount |
[] | 0 | 1 | empty coin set, but amount 0 needs zero coins — always 1 way |
[3,5,7] | 10 | 1 | only {3,7} works |
[1,2,3] | 4 | 4 | {1,1,1,1}, {1,1,2}, {2,2}, {1,3} |
(Critical distinction to flag upfront: this counts COMBINATIONS, order-independent — different from "Coin Change 1" which asks for MINIMUM coins, and different from counting order-dependent PERMUTATIONS, which is a related-but-distinct problem.)
amount = 0? (Standard convention: 1, representing "the empty combination.")amount and number of coin types?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