60. Subsets
mediumAsked at RedditGenerate all 2^n subsets of a set of distinct integers. Reddit uses this to test backtracking and bitmask enumeration — the same enumeration used when computing all flag-combination buckets for feature-rollout cohorts.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Reddit loops.
- Glassdoor (2026-Q1)— Reddit on-site favorite for backend roles.
- Blind (2025-12)— Reported in Reddit infra-team rounds.
Problem
Given an integer array nums of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order.
Constraints
1 <= nums.length <= 10-10 <= nums[i] <= 10All the numbers of nums are unique.
Examples
Example 1
nums = [1,2,3][[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]Example 2
nums = [0][[],[0]]Approaches
1. Iterative power-doubling
Start with [[]]. For each number, append it to copies of existing subsets.
- Time
- O(n * 2^n)
- Space
- O(n * 2^n)
function subsets(nums) {
let result = [[]];
for (const n of nums) {
const next = result.map(s => [...s, n]);
result = [...result, ...next];
}
return result;
}Tradeoff: Clean. Doubles output per element.
2. Backtracking with start-index (optimal)
Recurse picking any i >= start. Push current at every entry.
- Time
- O(n * 2^n)
- Space
- O(n) recursion
function subsets(nums) {
const out = [];
function backtrack(start, current) {
out.push([...current]);
for (let i = start; i < nums.length; i++) {
current.push(nums[i]);
backtrack(i + 1, current);
current.pop();
}
}
backtrack(0, []);
return out;
}Tradeoff: Memory-efficient (current is shared).
Reddit-specific tips
Reddit interviewers will accept either. Bonus signal: mention the bitmask version (iterate i from 0 to 2^n; for each bit b in i, include nums[b]) and discuss when it's cleaner.
Common mistakes
- Forgetting to push the empty subset.
- Pushing current directly without copy.
- Using start=current.length+1 (off by one).
Follow-up questions
An interviewer at Reddit may pivot to one of these next:
- Subsets II (LC 90) — with duplicates.
- Permutations (LC 46).
- Combinations (LC 77).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Bitmask version?
for (let mask=0; mask<(1<<n); mask++) collect nums[b] for each set bit b. Useful when you want subsets in deterministic order.
Why push current at the start of backtrack?
Each subset is the snapshot of current at some recursion entry — including the empty one at depth 0.
Practice these live with InterviewChamp.AI
Drill Subsets and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →