DSA interview question
A board game gives each player a handful of number cards, and a special move lets them play any subset of their cards and score its total. To show a player every total they could possibly score, the game needs the sum of every one of the 2ⁿ subsets, including the empty subset (0), listed in increasing order. Equal totals from different subsets are listed separately. It's the pick/skip recursion, carrying only a running sum instead of the whole subset.
Example: [2, 3] → 0 2 3 5.
vector<int> subsetSums(const vector<int>& arr); // all 2^n sums, sorted ascending
| Input | Output | Explanation |
|---|---|---|
[2, 3] | 0 2 3 5 | {}, {2}, {3}, {2, 3} |
[5, 2, 1] | 0 1 2 3 5 6 7 8 | 8 subsets |
[3, 3] | 0 3 3 6 | The two single-card subsets both sum to 3 |
[1] | 0 1 | |
[] | 0 | Only the empty subset |
(Confirm: all 2ⁿ sums are returned, repeats included; the output is sorted ascending; n is at most about 15.)
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