DSA interview question
A café sells drinks at a few distinct price points, and a customer with a gift card of value target wants to see every way to spend it exactly, buying any drink as many times as they like. Order doesn't matter, so 2 + 2 + 3 and 3 + 2 + 2 are the same combination. Backtracking avoids those duplicates with one rule: once you move past a candidate, you never come back to it. At each step, either use the current candidate again (staying on it) or move on to the next one.
Example: candidates [2, 3, 6, 7], target 7 → [2,2,3], [7].
vector<vector<int>> combinationSum(const vector<int>& candidates, int target);
Input (candidates, target) | Output | Explanation |
|---|---|---|
([2, 3, 6, 7], 7) | [2,2,3] [7] | 2 is reused |
([2, 3, 5], 8) | [2,2,2,2] [2,3,3] [3,5] | |
([2], 1) | (none) | |
([1], 2) | [1,1] | |
([3, 4], 10) | [3,3,4] |
(Confirm: candidates are distinct positive integers; each can be used any number of times; combinations are unique as multisets; any order of output is fine.)
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