Generate all subsets of an array of distinct integers
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
Overview: Generate every subset of an array of distinct integers, with each subset listed in ascending order and the whole list sorted lexicographically. Tests building the power set by iteration or backtracking, copying partial results safely, and reasoning about exponential output size.
Constraints
- 1 <= len(nums) <= 10
- -10 <= nums[i] <= 10
- All elements of nums are distinct.
Examples
Input: ([0],)
Expected Output: [[], [0]]
Explanation: Singleton zero from the source's second example: the empty subset first, then the element alone.
Input: ([-7],)
Expected Output: [[], [-7]]
Explanation: Singleton negative value still gives two subsets with the empty one first.
Hints
- The input order never changes the answer, so you may rearrange `nums` into whatever order makes the rest easier.
- With n distinct values, every subset matches one yes or no choice per value, which gives 2^n subsets in total.
- Check your order against Example 1: `[1, 2]` and `[1, 2, 3]` both come before `[1, 3]`, and every subset that starts with 1 comes before `[2]`.