FastPrepSubsets

Subsets

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Given an integer array nums whose elements are unique, return all of its subsets, including the empty subset.

Within each subset, preserve the elements' original input order. Return the subsets in increasing binary-mask order: bit i indicates whether nums[i] belongs to that subset.

Function

subsets(nums: int[]) → int[][]

Examples

Example 1

nums = [1,2,3]return = [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

The eight outputs correspond to masks 000 through 111.

Example 2

nums = [0]return = [[],[0]]

A one-element array has the empty subset and the subset containing that element.

Constraints

  • 1 <= nums.length <= 10.
  • -10 <= nums[i] <= 10.
  • All values in nums are unique.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[][] subsets(int[] nums) {
    // Write your code here.
}
nums[1,2,3]
expected[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
Checking account…