DSA interview question
A pizza app lets customers pick any combination of toppings from a list of distinct toppings, including none at all. To pre-compute prices, the team needs every possible subset of the list, which is the power set. With n items there are 2ⁿ subsets, because each item is independently taken or not taken. The pick/skip recursion mirrors that exactly: at index i, branch once with nums[i] in the subset and once without it, and record the subset when the index reaches the end.
Example: [1, 2, 3] → [1,2,3] [1,2] [1,3] [1] [2,3] [2] [3] [].
vector<vector<int>> subsets(const vector<int>& nums); // any order; shown in take-then-skip order
| Input | Output (take-then-skip order) | Explanation |
|---|---|---|
[1, 2, 3] | [1,2,3] [1,2] [1,3] [1] [2,3] [2] [3] [] | 2³ = 8 subsets |
[0] | [0] [] | |
[1, 2] | [1,2] [1] [2] [] | |
[] | [] | Only the empty subset |
(Confirm: elements are distinct; the empty subset is included; any order of subsets 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