📖 3 min read
Last updated on

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 →

Decision tree for subsets of [1,2,3]: at each element choose include or skip, producing all 8 subsets at the leaf level.

What Interviewers Are Actually Testing

SkillWhat Interviewers Observe
Backtracking mechanicsCan you set up recursion + undo correctly?
Recursion treeCan you trace and explain the branches?
Output constructionDo you copy the current path before appending?
Bit manipulationDo 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

  1. Initialize results = [], current = [].
  2. Define backtrack(start): append a copy of current to results, then loop from start to end — add element, recurse with i+1, remove element.
  3. 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

AspectComplexity
TimeO(n × 2ⁿ) — 2ⁿ subsets, each up to length n
SpaceO(n) — recursion stack (not counting output)

Common Interview Mistakes

  1. Forgetting to copy current. results.append(current) appends a reference — all entries point to the same (eventually empty) list. Always results.append(list(current)).
  2. Looping from 0 instead of start. Revisiting earlier elements produces duplicates.
  3. 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.



Practice This in a Mock Interview

Start a mock interview for Subsets on Intervu.


Further reading:

Practice Like It's the Real Interview

Get instant feedback on your approach, communication, and code — powered by AI.

Start a Mock Interview →