← Questions

All subsets of a list with duplicates

0
GoogleCodingLast asked Jan 2026

A coding interview question asked in Google's onsite coding round, last asked Jan 2026. It tests backtracking and recursion.

Return every distinct subset of an integer list that may contain duplicate values. Two subsets are the same if they contain the same values with the same multiplicities, and each distinct subset must appear exactly once.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • subsets_with_dup(nums: list[int]) -> list[list[int]] — any order of subsets; each subset sorted.

Example

[1, 2, 2]     ->  [[], [1], [1, 2], [1, 2, 2], [2], [2, 2]]
[0]           ->  [[], [0]]
[2, 1, 2, 2]  ->  8 subsets   # 0 to 1 copies of 1, times 0 to 3 copies of 2

Constraints

  • 1 <= len(nums) <= 10; -10 <= nums[i] <= 10.

Clarifying questions

  • Does the order of subsets or of elements inside them matter?
  • Is the empty subset included?
  • Are subsets with the same values in a different order the same?

What a strong answer covers

  • Sort first so equal values are adjacent.
  • Backtracking that, at each depth, skips a value equal to the previous one at the same depth (j > i and nums[j] == nums[j-1]), so duplicates are never generated rather than removed afterwards.
  • The counting view: for a value with count c, choose 0 to c copies; the output size is the product of (c + 1).
  • An iterative version: when a value repeats, extend only the subsets created in the previous step.
  • O(n · 2^n) time in the worst case; why de-duplicating with a set of tuples works but wastes effort.

Follow-ups

  • Return only the subsets that sum to a target.
  • Return the subsets of size k.
  • Generate them lazily, one at a time.

Where it's asked

  • Google · Onsite coding · Last asked Jan 202645 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)

What it tests

backtracking · recursion

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding