Skip to content
CodeKruPro

All subsets of a list

Coding problemMediumAsked in: Adobe, Amazon, Apple, Google, Meta, Microsoft

Given a list of distinct integers, return every subset of it. A subset keeps some of the values (possibly none, possibly all).

The empty subset and the full list are both subsets. Return the subsets in any order; the values inside each subset may be in any order too. Each subset must appear exactly once.

Examples

Example 1
Input: nums = [1,2,3]
Output: [[],[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]]
Example 2
Input: nums = [0]
Output: [[],[0]]
Example 3
Input: nums = []
Output: [[]]
An empty list has exactly one subset: the empty one.

Constraints

  • The list holds between 0 and 15 integers
  • The values are distinct and between -1,000 and 1,000

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

to answer and keep your progress. Reading is free for everyone.