Asked at 12 companies
7 recent
- Bloomberg
- Amazon
- Meta
- Microsoft
- Agoda
- Infosys
- Coupang
- PornHub
- Wix
+2 moreShow fewer
- Uber
- PayPal
Community-reported · source · updated 2026-11-18. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Builds on: Building a new string or array as the answer · Replacing recursion with your own stack · Recursion and the call stack · The base case: where the calls stop · Depth-first traversal: preorder, inorder, postorder · Binary numbers and AND, OR, XOR
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Sudoku solvers and regex: Try a choice, go deeper, and undo it when it fails. Solvers fill puzzles this way, and many regex engines match patterns the same way.
ProblemProblem statementGiven an integer array nums of unique elements, return all possible subsets (the power set). The solution should not contain duplicate subsets.ReadHide
Problem Description
Given an integer array nums of unique elements, return all possible subsets (the power set). The solution should not contain duplicate subsets.
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Use a recursive function to explore all possibilities. At each element, make a choice: 1) Include the element and recurse. 2) Exclude the element and recurse. Backtrack by removing the element.
Approaches
1. Backtracking (Optimal) - O(N · 2^N) ⭐
Idea: The total number of subsets is 2^N, and copying each subset takes O(N) time, so the complexity is fixed. Backtracking simulates a Depth First Search (DFS) of the decision tree.
- Define a recursive function
backtrack(index, current_subset). - Base Case: When
indexreaches the array length, a full subset has been formed. Addcurrent_subsetto the results. - Recursive Step:
- Choice 1 (Include): Add
nums[index]tocurrent_subset. Recurse withindex + 1. - Backtrack: Remove
nums[index]fromcurrent_subset. - Choice 2 (Exclude): Recurse with
index + 1.
- Choice 1 (Include): Add
2. Bit Manipulation (Alternative Optimal) - O(N · 2^N)
Idea: Since there are 2^N subsets, iterate from 0 to 2^N - 1. Each integer's binary representation acts as a bitmask: if the j-th bit is set (1), include the j-th element from nums.
Solution code
import sys
from typing import List
class Solution:
# Optimized: O(N * 2^N) Time, O(N * 2^N) Space - Backtracking/DFS
def subsets_optimal(self, nums: List[int]) -> List[List[int]]:
results = []
def backtrack(index, current_subset):
# Base Case: All elements checked, save the subset
results.append(list(current_subset))
for i in range(index, len(nums)):
# 1. Choose (Include nums[i])
current_subset.append(nums[i])
# 2. Recurse
backtrack(i + 1, current_subset)
# 3. Backtrack (Exclude nums[i] for the next iteration at this depth)
current_subset.pop()
backtrack(0, [])
return results
# Alternative Optimal: O(N * 2^N) Time, O(N * 2^N) Space - Bit Manipulation
def subsets_bit_manipulation(self, nums: List[int]) -> List[List[int]]:
results = []
N = len(nums)
# Iterate through all 2^N masks
for i in range(1 << N):
subset = []
for j in range(N):
# Check if the j-th bit is set in the mask i
if (i >> j) & 1:
subset.append(nums[j])
results.append(subset)
return resultsOther ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language