Top 150 InterviewEasy

Subsets

Detailed guide and Python implementation for the 'Subsets' problem.

Problem Statement

Easy

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.

Implement a function subsets(nums: list) -> list.

Constraints
  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • All the numbers of nums are unique

Examples

Example 1
Input
[1,2,3]
Output
[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Explanation

All 2^3 = 8 subsets of [1,2,3] are generated, including the empty set and the full set.

Example 2
Input
[0]
Output
[[],[0]]
Explanation

The two subsets of [0] are the empty set [] and [0].

Need a Hint?
Consider using Backtracking-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.