DSA interview question
A vending machine that only takes exact change holds a list of coins, each coin usable once. Given a price K, can some subset of the coins add up to exactly K? This is the classic subset-sum decision problem. The recursion is the pick/skip pattern that stops at the first success: return true the moment the running sum reaches K, and never explore further once any branch has succeeded. Because the state is just (index, sum), the recursion also turns into an O(n · K) boolean table.
Example: [3, 34, 4, 12, 5, 2], K = 9 → true (4 + 5).
bool isSubsetSum(const vector<int>& arr, int k); // non-negative values
Input (arr, K) | Output | Explanation |
|---|---|---|
([3, 34, 4, 12, 5, 2], 9) | true | 4 + 5 (or 3 + 4 + 2) |
([3, 34, 4, 12, 5, 2], 30) | false | Everything except 34 only sums to 26 |
([1, 2, 3], 6) | true | All three |
([2, 4, 6], 5) | false | Only even sums are possible |
([8], 8) | true |
(Confirm: each element can be used at most once; values are non-negative; K = 0 is always reachable with the empty subset.)
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