Generate all subsets of an array of distinct integers

Quick Overview

Generate every subset of an array of distinct integers, with each subset listed in ascending order and the whole list sorted lexicographically. Tests building the power set by iteration or backtracking, copying partial results safely, and reasoning about exponential output size.

Generate all subsets of an array of distinct integers

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an integer array `nums` whose elements are all distinct, return every subset of `nums` (the power set). Each subset must appear exactly once, and the empty subset counts as a subset. ### Function Signature ```python def subsets(nums: list[int]) -> list[list[int]]: ``` ### Rules - List the elements inside each subset in ascending order. - Order the subsets lexicographically, the way Python compares lists: element by element from the left, and a list that is a prefix of a longer list comes first. The empty subset is therefore always first. - The order of `nums` in the input does not change the output. ### Constraints - `1 <= len(nums) <= 10` - `-10 <= nums[i] <= 10` - All elements of `nums` are distinct. ### Examples ```text Input: nums = [1, 2, 3] Output: [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]] ``` Three distinct elements give 8 subsets, one for each way to include or skip each element. `[1]` comes before `[1, 2]` because it is a prefix, and `[1, 2, 3]` comes before `[1, 3]` because 2 < 3 at the second position. ```text Input: nums = [0] Output: [[], [0]] ``` One element gives two subsets: the empty one and the element on its own. ```text Input: nums = [3, -1] Output: [[], [-1], [-1, 3], [3]] ``` The two-element subset is written `[-1, 3]` because elements inside a subset are ascending whatever the input order. It sorts before `[3]` because -1 < 3 at the first position.

Overview: Generate every subset of an array of distinct integers, with each subset listed in ascending order and the whole list sorted lexicographically. Tests building the power set by iteration or backtracking, copying partial results safely, and reasoning about exponential output size.

General logo
General
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums whose elements are all distinct, return every subset of nums (the power set). Each subset must appear exactly once, and the empty subset counts as a subset.

Function Signature

def subsets(nums: list[int]) -> list[list[int]]:

Rules

  • List the elements inside each subset in ascending order.
  • Order the subsets lexicographically, the way Python compares lists: element by element from the left, and a list that is a prefix of a longer list comes first. The empty subset is therefore always first.
  • The order of nums in the input does not change the output.

Constraints

  • 1 <= len(nums) <= 10
  • -10 <= nums[i] <= 10
  • All elements of nums are distinct.

Examples

Input: nums = [1, 2, 3]
Output: [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]

Three distinct elements give 8 subsets, one for each way to include or skip each element. [1] comes before [1, 2] because it is a prefix, and [1, 2, 3] comes before [1, 3] because 2 < 3 at the second position.

Input: nums = [0]
Output: [[], [0]]

One element gives two subsets: the empty one and the element on its own.

Input: nums = [3, -1]
Output: [[], [-1], [-1, 3], [3]]

The two-element subset is written [-1, 3] because elements inside a subset are ascending whatever the input order. It sorts before [3] because -1 < 3 at the first position.