Subsets — Coding Interview Walkthrough
You’re in a coding interview. The interviewer says: “Given an array of unique integers, return all possible subsets.” You know it’s backtracking. But now you need clean code, a clear explanation of the recursion tree, and — if you’re sharp — the bit manipulation alternative. Subsets is the gateway to the entire backtracking family.
The Problem
Given an integer array nums of unique elements, return all possible subsets (the power set). The solution must not contain duplicate subsets.
Example
Input: nums = [1, 2, 3]
Output: [[], [1], [2], [1,2], [3], [1,3], [2,3], [1,2,3]]
Already comfortable with the solution? Practice it in a mock interview →
What Interviewers Are Actually Testing
| Skill | What Interviewers Observe |
|---|---|
| Backtracking mechanics | Can you set up recursion + undo correctly? |
| Recursion tree | Can you trace and explain the branches? |
| Output construction | Do you copy the current path before appending? |
| Bit manipulation | Do you know the iterative approach? |
Step 1: The Key Insight
Every subset corresponds to a unique decision sequence: for each element, include it or skip it. Backtracking models this naturally — at each index, branch into two paths, recurse, then undo.
Step 2: Algorithm
- Initialize
results = [],current = []. - Define
backtrack(start): append a copy ofcurrentto results, then loop fromstartto end — add element, recurse withi+1, remove element. - Call
backtrack(0).
Python Implementation
Backtracking:
def subsets(nums: list[int]) -> list[list[int]]:
results = []
current = []
def backtrack(start: int) -> None:
results.append(list(current))
for i in range(start, len(nums)):
current.append(nums[i])
backtrack(i + 1)
current.pop()
backtrack(0)
return results
Iterative:
def subsets_iterative(nums: list[int]) -> list[list[int]]:
results = [[]]
for num in nums:
results += [subset + [num] for subset in results]
return results
Time & Space Complexity
| Aspect | Complexity |
|---|---|
| Time | O(n × 2ⁿ) — 2ⁿ subsets, each up to length n |
| Space | O(n) — recursion stack (not counting output) |
Common Interview Mistakes
- Forgetting to copy
current.results.append(current)appends a reference — all entries point to the same (eventually empty) list. Alwaysresults.append(list(current)). - Looping from 0 instead of
start. Revisiting earlier elements produces duplicates. - Confusing with Subsets II. Subsets II has duplicate numbers — needs sorting + skip logic. This problem guarantees unique elements.
What a Strong Candidate Sounds Like
“I’ll use backtracking. At each call, I snapshot the current path and add it to results. Then I loop from start forward, add each element, recurse deeper, and pop afterward. Time is O(n × 2ⁿ).”
Example Interview Dialogue
Interviewer: Walk me through the recursion tree for [1, 2, 3].
Candidate: The root captures []. We branch on 1: including it gives [1], from which we branch on 2 giving [1,2], then on 3 giving [1,2,3]. Each branch doubles the subsets — 8 total.
Related Problems to Practice
- Combination Sum — Backtracking with target sum.
- Permutations — Backtracking for ordered arrangements.
- Word Break — Decision-tree structure with memoization.
Practice This in a Mock Interview
Start a mock interview for Subsets on Intervu.
Further reading:
- How to Prepare for a Coding Interview in 2026, the complete roadmap
- Why LeetCode alone isn’t enough, and what to practice instead
- Practice any LeetCode problem as a live mock interview
- The Grind 75 Pathway, a structured study plan with AI practice links
- Browse all walkthroughs on GitHub