Back to Practice
#0150

Generate All Subsets

MediumDSA25 min20 XP

Problem

Given a list of distinct numbers, return all subsets. The order of subsets does not matter.

Why This Matters

Subsets are the simplest backtracking pattern and prepare learners for combinations, search, and constraint solving.

Function Signature

def subsets(nums):

Examples

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

For each number, the recursion chooses either exclude or include.

Constraints

  • Return the exact requested value.
  • Handle empty or small inputs when the prompt allows them.
  • Keep the code readable and deterministic.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input[1, 2]
Expected[[], [2], [1], [1, 2]]

For each number, the recursion chooses either exclude or include.

Hidden Test Categories
empty or smallest inputduplicates or repeated valueslarger realistic input