DSA Tracker

Medium

Subsets

A medium Backtracking problem included in Apna College, Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Backtracking
Sheets
3
Core for
0 roles
Platform
LeetCode

The problem

Given an array of distinct integers, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Subsets can be returned in any order.

Example 1

Input
nums = [1, 2, 3]
Output
[[], [1], [2], [3], [1,2], [1,3], [2,3], [1,2,3]]
Why
The power set of {1,2,3} contains 2^3 = 8 subsets, including the empty set and the set itself.

Example 2

Input
nums = [0]
Output
[[], [0]]
Why
The power set of {0} contains 2 subsets: the empty set and the set containing 0.

Constraints

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • All numbers in nums are distinct

How to think about it

Updated 2026-09-09

Every element in the array faces an independent binary decision: either it joins the subset or it stays out. Viewing the power set as a full binary decision tree of depth n reveals that every leaf is a valid subset, meaning you never need to prune or search for a target — you only record what you hold at each step.

Approaches, worst first

  1. Cascading iterative addition

    time O(n * 2^n) · space O(n * 2^n)

    Start with an empty subset. For every number in the input, clone every existing subset, append the number to the clones, and merge them back. Simple to trace, but requires frequent reallocation and copying of intermediate lists.

  2. Bitmask iteration

    time O(n * 2^n) · space O(1)

    Map each integer from 0 to 2^n - 1 to a subset where the j-th bit indicates inclusion of nums[j]. Non-recursive and avoids call stack overhead, but tests n bits for every single subset even when most bits are zero.

  3. Backtracking pick or skipWrite this one

    time O(n * 2^n) · space O(n)

    Maintain a single mutable buffer. At index i, branch into skipping nums[i] and taking nums[i], snapshotting the buffer once reaching index n. Mutating in place keeps extra working memory strictly bounded by the recursion depth.

Where people lose marks · 3
  • Pushing a reference of the working list directly into the output instead of a shallow copy. Because the same list is mutated and emptied later, every captured subset ends up empty.
  • Forgetting to pop the element after exploring the branch that included it, which bleeds elements into sibling branches.
  • Overlooking the base subset: an empty list is a required member of the power set and must be recorded when recursion reaches the end without choices.

The theory behind it

Backtracking — the ground this problem stands on. All Backtracking problems

What Backtracking is

Backtracking is an organized trial-and-error search through a maze of possibilities. You make a tentative choice, move forward to explore where that path leads, and if you hit a dead end or finish finding an answer, you back up and undo that choice. By cleaning up your changes before trying the next option, a single shared board or list is explored thoroughly without needing to clone full copies of your data at every turn.

When to reach for it

Reach for backtracking when a problem asks to generate all possible solutions, like all subsets, permutations, valid parentheses combinations, or word search paths on a board. Signals include puzzles with strict constraint rules, like placing eight non-attacking queens on a chessboard or solving a Sudoku grid. Whenever you must construct combinations step by step and abandon dead-end branches early before wasting time exploring impossible paths, use backtracking.

How the pattern works

Follow a three-step rhythm inside a loop: choose, explore, and unchoose. First, check if the current state satisfies your goal; if so, save a copy of it and return. Next, prune illegal moves immediately using constraint checks so unpromising branches are skipped. For each valid candidate, apply the move to your shared path or board, call the recursive function to explore deeper, and finally undo the move right after the call returns. Undoing restores the shared state so sibling choices start from a clean slate.

What each operation costs

OperationTime
generate all subsets of n elementsO(2^n)
generate all permutations of n elementsO(n!)
auxiliary recursion stack memory depthO(n)
What usually goes wrong with Backtracking
  • Adding a mutable path list directly to the final answers collection without creating a shallow copy, leaving every saved result empty once backtracking finishes.
  • Forgetting to undo a state change after the recursive call returns, contaminating subsequent branches with leftover moves from earlier paths.
  • Generating duplicate subsets or permutations by failing to sort the input array and skip adjacent identical elements during branch selection.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Backtracking problems

Problem set and role mapping as of .