Subsets Pattern
The Subsets pattern enumerates every combination, permutation, or arrangement of a small set of elements by either iteratively extending an existing list of subsets with each new element or by recursing through a decision tree (include / exclude). It powers backtracking problems on combinations, permutations, and string letter cases in O(n * 2^n) time and O(n * 2^n) space.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Complexity
- Time
- O(n * 2^n)
- Space
- O(n * 2^n)
When to use Subsets
- The problem asks for all subsets, all permutations, or all combinations of a small input.
- You need to generate every possible string that satisfies a structural constraint (parentheses, IP addresses, letter case).
- You can frame the answer as a sequence of include/exclude decisions on each input element.
- The input size is small (n <= 20 for subsets, n <= 10 for permutations) because the output is exponential.
- Counting alone is not enough — you must enumerate the actual combinations or paths.
Template
function subsets(nums) {
const result = [[]];
for (const num of nums) {
const size = result.length;
for (let i = 0; i < size; i++) {
result.push([...result[i], num]);
}
}
return result;
}Example LeetCode problems
| # | Problem | Difficulty | Frequency |
|---|---|---|---|
| 78 | Subsets | medium | foundational |
| 90 | Subsets II | medium | frequently asked |
| 46 | Permutations | medium | foundational |
| 47 | Permutations II | medium | frequently asked |
| 77 | Combinations | medium | foundational |
| 39 | Combination Sum | medium | company favorite |
| 784 | Letter Case Permutation | medium | classic |
| 22 | Generate Parentheses | medium | company favorite |
Common mistakes
- Iterating over `result` with a normal length read in the inner loop, which grows the array as you append and creates an infinite loop.
- Forgetting to make a shallow copy of the partial subset before pushing it into the result, which means every saved subset points to the same mutating array.
- On Subsets II, failing to sort the input first and skip duplicates — duplicates produce identical subsets that violate the unique-output requirement.
- Using the recursive include/exclude template without a depth parameter, causing the recursion to revisit indices.
- Pruning too aggressively in Combination Sum and missing valid combinations that use the same number multiple times.
Frequently asked questions
What is the difference between subsets, permutations, and combinations?
Subsets ignore order and any element can be present or absent (2^n total). Permutations care about order (n! total). Combinations pick k of n without regard to order (n choose k total).
When should I use iteration vs recursion for subsets?
Iteration is cleaner for the basic Subsets problem because there's no branching decision tree to express. Recursion is clearer for permutations, combination-sum, and any variant that prunes branches early.
Why is the time complexity O(n * 2^n) and not O(2^n)?
There are 2^n subsets, but copying each one into the result takes O(n) time in the worst case. Multiplying gives O(n * 2^n) for both time and output size.
How do I avoid duplicate subsets when the input has repeats?
Sort the input first so duplicates are adjacent. When generating subsets, only extend previously-added subsets if the current element differs from the previous, or only extend the subsets added in the last iteration if the previous element was a duplicate.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill the Subsets pattern under real interview conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →