FrontendInterviews.dev

Loading problem…

302. Subsets

Medium•
Acceptance: 75.00%
•
🔓3/3 Pro unlocks today

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.

Generate all possible subsets of a set. This is the foundational backtracking problem that teaches the include/exclude decision pattern.

Requirements

1. Basic Functionality

  • Return all 2^n subsets including the empty set
  • No duplicate subsets
  • Order within subsets and between subsets doesn't matter

Example Usage

subsets([1, 2, 3]);
// [[], [1], [2], [1,2], [3], [1,3], [2,3], [1,2,3]]

Real-World Context

  • Feature toggles: Enumerating all possible feature flag combinations
  • Test coverage: Generating all combinations for exhaustive testing
  • Menu selections: All possible meal combinations

Constraints

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