Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Medium Backtracking High frequency Solution Available Practice Mode

Subsets LeetCode Solution

Tags: #Array #Backtracking #Bit Manipulation

Approach Summary

At each index, two choices: include or exclude. Backtrack by adding element, recursing, removing element.

Practice Mode

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Backtracking Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Backtracking pattern.

Framework: Backtracking • Interview Prep: Tested at FAANG • Explore all Backtracking problems & invariants →

How to Recognize This Pattern

  • Generate all subsets/power set
  • Include/exclude pattern

Complexity Analysis

Time Complexity

O(n × 2ⁿ)

Space Complexity

O(n)

Tags

Array Backtracking Bit Manipulation

Frequently Asked Questions

What is the optimal time and space complexity for Subsets?
The optimal solution for Subsets achieves O(n × 2ⁿ) runtime complexity and requires O(n) auxiliary space. There are 2ⁿ subsets, each costing up to O(n) to copy — O(n · 2ⁿ) time. The recursion stores the path (depth ≤ n) plus the 2ⁿ subsets in the output — O(n · 2ⁿ) space.
How do I identify that Subsets requires the Backtracking pattern?
In technical interviews, look for key problem signals: Generate all subsets/power set; Include/exclude pattern. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Backtracking strategy.
What critical edge cases must be handled in Subsets?
Essential edge cases to verify include: Empty input — returns [[]] (the empty subset); Duplicate values — subsets are still distinct by position, e.g. [1] appears twice for [1, 1]; Single element — [[], [x]]; Large n — 2ⁿ explodes; problem constraints keep n around 10–20. Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Subsets?
Subsets is a Medium-difficulty question tagged with high recurrence. It is actively asked in technical screening and onsite interview loops at Facebook, Amazon, Bloomberg.
Buy me a coffee
Daily Practice
0/0