Subsets II
Updated
Problem statement
You are given an integer array nums that may contain repeated values. Return every distinct subset of nums, including the empty subset.
A subset is any choice of some of the elements. Two subsets are the same when they hold the same values the same number of times, no matter which positions the values came from. Return each distinct subset once.
Write every subset in increasing order. Return the subsets in dictionary order: compare two subsets value by value from the front, and the one with the smaller value at the first difference comes first. If one subset is the start of the other, the shorter one comes first. So the empty subset is always first.
Function
subsetsWithDup(nums: int[]) → List<List<Integer>>Examples
Example 1
nums = [2,1,2]return = [[],[1],[1,2],[1,2,2],[2],[2,2]]Sort the values first: 1, 2, 2. The distinct subsets, in the required order, are:
[], the empty subset.[1],[1, 2]and[1, 2, 2], all starting with 1.[2]and[2, 2], starting with 2.
Picking either 2 gives the same subset, so [2] and [1, 2] each appear once. There are 6 subsets: 2 choices for the 1 (take it or not) times 3 choices for the 2s (none, one or both).
Example 2
nums = [5,5,5]return = [[],[5],[5,5],[5,5,5]]Only the number of 5s matters: none, one, two or three. So there are 4 subsets: [], [5], [5, 5] and [5, 5, 5].
Example 3
nums = [-1,3]return = [[],[-1],[-1,3],[3]]The values are different, so every choice is its own subset. There are 4 subsets. In order they are [], [-1], [-1, 3] and [3].
Example 4
nums = [4,4,1,1]return = [[],[1],[1,1],[1,1,4],[1,1,4,4],[1,4],[1,4,4],[4],[4,4]]Sort the values: 1, 1, 4, 4. There are 3 choices for the 1s (none, one, two) and 3 for the 4s, so 9 subsets.
The ones that start with 1 come first: [1], [1, 1], [1, 1, 4], [1, 1, 4, 4], [1, 4] and [1, 4, 4]. Then come [4] and [4, 4], after the empty subset [] at the very front.
Constraints
1 <= nums.length <= 10-10 <= nums[i] <= 10- Values in
numsmay repeat.
More Bloomberg LP Onsite Interview questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Replacements to Make Two Strings AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Currency Conversion RatePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Bounded Combination Sum with Signed CandidatesConfluent · ONSITE INTERVIEWConfluent interview questions
- Enumerate Character Paths to a MarkerTesla · ONSITE INTERVIEWTesla interview questions
- Generate Circular Student ArrangementsAmazon · ONSITE INTERVIEW
- Enumerate Compatible Meeting SubsetsExpedia · ONSITE INTERVIEWExpedia interview questions
- Partition into Equal-Sum GroupsPalo Alto Networks · ONSITE INTERVIEWPalo Alto Networks interview questions
- PermutationsEPAM · PHONE SCREENEPAM interview questions
- Combination Sum with Single-Use ValuesPinterest · PHONE SCREENPinterest interview questions
- Combination Sum with Unlimited ReuseNVIDIA · PHONE SCREENNVIDIA interview questions
- Sort by Checking PermutationsMercor · PHONE SCREENMercor interview questions
- Enumerate Valid Clock TimesMicrosoft · OAMicrosoft interview questions
Bloomberg LP interview experiences
- Bloomberg SWE Phone Screen and Onsite: 1 Experience
- Bloomberg SWE Phone Screens with Two Teams: 1 Experience
- Bloomberg AI Interview, Hiring Level Not Reported: 2 Experiences
- Bloomberg Experienced SWE AI Coding Interviews: 2 Experiences
- Bloomberg New Grad AI-Assisted Coding Rounds: 3 Experiences
- Bloomberg New Grad SWE AI-Assisted Coding Rounds: 1 Experience