Skip to content
MediumBacktrackingAI interview only

Subsets

Asked atmetaamazonmicrosoftbloomberguberadobe

01 · Problem

Given an integer array nums of unique elements, return all possible subsets (the power set).

The solution set must not contain duplicate subsets. Return the solution in any order.

02 · Examples

Example 01
Input
nums = [1,2,3]
Output
[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

The power set of [1,2,3] includes all 8 subsets, from the empty set to the full array.

Example 02
Input
nums = [0]
Output
[[],[0]]

With a single element, there are only two subsets: the empty set and the set containing 0.

Example 03
Input
nums = [1,2]
Output
[[],[1],[2],[1,2]]

The power set of [1,2] has 4 subsets.

03 · Constraints

  • 011 <= nums.length <= 10
  • 02-10 <= nums[i] <= 10
  • 03All the numbers of nums are unique

04 · Optimal complexity

Time
O(n * 2^n)
Space
O(n * 2^n)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.