By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"This pattern appears in 1 out of every 3 onsite interviews—master it, and you’ll instantly stand out as a candidate who thinks in recursion, backtracking, and combinatorial logic."
Before tackling subsets, ensure you understand: 1. Recursion & Backtracking – How to build solutions incrementally and undo choices. 2. Bitmasking – Representing subsets as binary numbers (optional but powerful). 3. Time/Space Complexity of Recursive Calls – How to analyze exponential-time algorithms.
1 << n
if i > start and nums[i] == nums[i-1]
Follow these steps every time you see a "generate all subsets" problem:
Are there constraints on subset size? (If yes, modify recursion depth.)
Choose the Right Approach
Duplicates in input? → Sort and skip duplicates in recursion.
Backtracking Template (Recursive)
path = []
Base case: When all elements are processed, add the current subset to the result.
Bitmasking Template (Iterative)
0
2^n - 1
For each bit in the number, if set, include the corresponding element.
Optimize for Duplicates (If Needed)
In recursion, skip duplicates by checking if i > start and nums[i] == nums[i-1].
Edge Cases
[[]]
[[], [element]]
All duplicates → Ensure no duplicate subsets in the result.
Time & Space Complexity
O(2^n)
2^n
O(n)
Problem: Given an integer array nums of unique elements, return all possible subsets (the powerset).
nums
def subsets(nums): res = [] def backtrack(start, path): res.append(path.copy()) # Add current subset for i in range(start, len(nums)): path.append(nums[i]) # Include nums[i] backtrack(i + 1, path) # Recurse path.pop() # Exclude nums[i] (backtrack) backtrack(0, []) return res
start
Problem: Given an integer array nums that may contain duplicates, return all possible subsets (the powerset). The solution set must not contain duplicate subsets.
def subsetsWithDup(nums): nums.sort() # Sort to handle duplicates res = [] def backtrack(start, path): res.append(path.copy()) for i in range(start, len(nums)): # Skip duplicates if i > start and nums[i] == nums[i-1]: continue path.append(nums[i]) backtrack(i + 1, path) path.pop() backtrack(0, []) return res
Problem: Given an integer array nums of unique elements, return all possible subsets of size k.
k
path
def subsetsOfSizeK(nums, k): res = [] def backtrack(start, path): if len(path) == k: res.append(path.copy()) return for i in range(start, len(nums)): path.append(nums[i]) backtrack(i + 1, path) path.pop() backtrack(0, []) return res
O(C(n, k))
n
O(k)
path.copy()
path[:]
res
if len(path) == k
i
[1,1]
i + 1
[1,2,2]
path.pop()
n ≤ 20
for mask in range(1 << n)
len(path) == k
"Alright, let’s lock this in. When you see ‘generate all subsets,’ remember:
Now go crush that interview. You’ve got this!
This guide is 100% interview-ready—use it as a checklist, and you’ll never blank on a subsets problem again. ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.