Subsets
MediumThis is an extra practice problem. It doesn't have walkthrough slides. You can solve it with the pattern template you already know. Open the hints if you get stuck.
Question
Given a list of distinct integers, build every grouping you can make by picking any number of values from the list. This includes a grouping with nothing in it and a grouping with every value in it.
Return all of these groupings together as a single list. The order of the groupings doesn't matter. The order of the values inside each grouping doesn't matter either.
Input: nums = [1, 2]
Output: [[], [1], [2], [1, 2]]
There are 4 groupings: the empty grouping, each single value on its own, and both values together.
Input: nums = [4, 9, 5]
Output: [[], [4], [9], [5], [4, 9], [4, 5], [9, 5], [4, 9, 5]]
You can either include or skip each of the 3 values. That gives 2 * 2 * 2 = 8 groupings.
Input: nums = []
Output: [[]]
An empty list still has one grouping: the empty grouping itself.
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
Remember to understand the problem and plan your approach before you start coding.