60. Subsets
mediumAsked at SalesforceReturn all possible subsets of a distinct-integer array. Salesforce uses this to test power-set enumeration via backtracking or bitmask.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Salesforce loops.
- Glassdoor (2026-Q1)— Salesforce uses subset enumeration for feature-flag combination testing.
- Blind (2025-11)— Recurring on Salesforce backend phone screens.
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 bitmask
Enumerate i = 0 to 2^n - 1; for each i, include nums[j] if bit j is set.
- Time
- O(2^n * n)
- Space
- O(2^n * n)
function subsets(nums) {
const result = [];
const n = nums.length;
for (let mask = 0; mask < (1 << n); mask++) {
const sub = [];
for (let i = 0; i < n; i++) if (mask & (1 << i)) sub.push(nums[i]);
result.push(sub);
}
return result;
}Tradeoff: Elegant for n <= 30 (fits in 32-bit int). Slightly less intuitive for non-bitmask candidates.
2. Backtracking
For each index, either include or exclude. Emit current path at every level.
- Time
- O(2^n * n)
- Space
- O(n) stack
function subsets(nums) {
const result = [];
function dfs(start, path) {
result.push([...path]);
for (let i = start; i < nums.length; i++) {
path.push(nums[i]);
dfs(i + 1, path);
path.pop();
}
}
dfs(0, []);
return result;
}Tradeoff: Idiomatic backtracking. The emit happens at EVERY level (not just leaves), capturing all subset sizes.
Salesforce-specific tips
Salesforce uses subset enumeration for feature-flag combination testing (try every subset of feature flags to find conflicts). They grade on whether you reach for the right enumeration pattern. Bonus signal: discuss that bitmask is preferred when n <= 32 and the result needs to be deterministic; backtracking is preferred otherwise.
Common mistakes
- Pushing path directly instead of slicing — all subsets end up identical.
- Emitting only at leaves — misses subsets of intermediate size.
- Using start = 0 instead of i+1 — generates duplicates.
Follow-up questions
An interviewer at Salesforce may pivot to one of these next:
- Subsets II (LC 90) — with duplicates.
- Generate all subsets summing to target.
- Subset Sum (LC 416).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why emit at every level, not just at the leaves?
Because every prefix of a valid path IS a valid subset. The empty path is the empty subset; [1] is a 1-element subset; etc.
Bitmask or backtracking — when to use which?
Bitmask is cleaner when n is small (<= 30). Backtracking generalizes better when you need pruning or non-binary choices per element.
Practice these live with InterviewChamp.AI
Drill Subsets and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →