Subsets
Problem
Given an array of distinct integers nums, produce every possible subset of its elements, including the empty set and the full set. The order of subsets in the output, and the order of elements within a subset, does not matter, but no subset may appear twice.
- Up to 12 elements.
- Each value is between -15 and 15.
- All elements are distinct.
Examples
nums = [5, 6, 9]→[[], [5], [6], [9], [5,6], [5,9], [6,9], [5,6,9]]in any order.2^3 = 8subsets.nums = [3]→[[], [3]].nums = [2, 8]→[[], [2], [8], [2,8]].
Hints
- For each element you make an independent yes/no decision. How many total outcomes does that give, and how would you enumerate them?
- Build subsets incrementally: after deciding about the first
ielements, recurse on the rest. Every partial path is itself a valid subset. - Backtracking with a start index (only add elements after the last one chosen) guarantees each subset is generated exactly once. A bit-mask loop from 0 to 2^n - 1 is the iterative alternative.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.