L2 · Working engineerRecursion~10 min · 4 tests#53

Generate All Subsets (Power Set)

Generate every subset of a list with recursion or iterative doubling in O(n·2ⁿ). Classic backtracking question with order-insensitive tests.

The problem

Return every subset of nums (values are distinct), including the empty set. Order of subsets, and inside each subset, does not matter.

Examples

  1. Example 1

    Input

    subsets([1, 2, 3])

    Expected output

    [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
  2. Example 2

    Input

    subsets([0])

    Expected output

    [[], [0]]

+ 2 hidden tests on Submit.

Edge cases to ask about

  • Empty input
  • Single element

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · subsets
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and read why.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Include/exclude recursionO(n · 2ⁿ)O(n · 2ⁿ)Each element doubles the number of subsets.
    Iterative doublingO(n · 2ⁿ)O(n · 2ⁿ)Start with [[]]; for each x, add x to every subset so far.
    bestBitmasks 0..2ⁿ−1O(n · 2ⁿ)O(n · 2ⁿ)Bit i set ⇒ include nums[i].
    Walkthrough of the optimal approach (try it yourself first)

    Start with [[]]. For each x, every existing subset spawns a twin with x appended, doubling the list. After n elements there are 2ⁿ subsets.

    Building [s + [x] for s in result] before the += matters — iterating over result while appending to it would loop forever.

    Complexity: O(n · 2ⁿ) time, O(n · 2ⁿ) space. There are 2ⁿ subsets, each up to n long.

    Reveal the reference solution
    def subsets(nums):
        result = [[]]
        for x in nums:
            result += [s + [x] for s in result]
        return result

    Follow-ups interviewers ask

    • Input has duplicates — no duplicate subsets.
    • Only subsets of size k (combinations).

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Generate All Subsets (Power Set) in Python?

    The optimal solution runs in O(n · 2ⁿ) time and O(n · 2ⁿ) auxiliary space. There are 2ⁿ subsets, each up to n long.

    What is the brute-force approach, and how do you optimise it?

    Include/exclude recursion: O(n · 2ⁿ) time, O(n · 2ⁿ) space. Each element doubles the number of subsets. Iterative doubling: O(n · 2ⁿ) time, O(n · 2ⁿ) space. Start with [[]]; for each x, add x to every subset so far. Bitmasks 0..2ⁿ−1: O(n · 2ⁿ) time, O(n · 2ⁿ) space. Bit i set ⇒ include nums[i].

    What follow-up questions do interviewers ask about Generate All Subsets (Power Set)?

    Input has duplicates — no duplicate subsets. Only subsets of size k (combinations).