Skip to content
Hello Python

Checking your account…

Sign in to save your code and progress across devices. The lesson and problem statement remain public.

Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace

Problem

Implement Solution.subsetsWithDup(nums). Return every distinct subset, including empty. Subset and value order do not matter.

Starter code

class Solution:
    def subsetsWithDup(self, nums):
        pass
Test cases

duplicate-two

{
  "args": [
    [
      1,
      2,
      2
    ]
  ]
}

Expected: [[],[1],[2],[1,2],[2,2],[1,2,2]]

Wizard outline
  1. Step 1: Initialize Solution.subsetsWithDup

    Replace the empty starter with the first real state owned by Solution.subsetsWithDup. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.

  2. Step 2: Assemble the primary transition

    Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.

  3. Step 3: Pass the Empty Input case

    Complete the readable core algorithm for one representative Interview case. Sort and skip equal sibling choices while allowing equal values at deeper levels.

  4. Step 4: Harden the Duplicate Two boundary

    Repair the reviewed boundary and pass the complete submission contract. Sorting groups equal choices. The first copy at each depth represents all subsets choosing that value there; deeper copies remain selectable, producing each multiplicity once.

Footguns and prerequisites
  • The duplicate condition is index > start, not index > 0.
  • recursion and backtracking
Reviewed references
Practice prerequisites
  • Complete One Backtracking Frame(opens in a new tab)

    Complete One Backtracking Frame isolates before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element. That focused state discipline is required when implementing subsets two as a complete Interview Problem.

Recommended approach and implementation

Sort, record every path, and skip a value equal to its predecessor when both are candidate siblings at the same depth.

Why it works: Sorting groups equal choices. The first copy at each depth represents all subsets choosing that value there; deeper copies remain selectable, producing each multiplicity once.

class Solution:
    def subsetsWithDup(self, nums):
        """
        Checkpoint 1: initialize the state owned by this Interview contract.
        Checkpoint 2: assemble the primary transition without hiding the boundary.
        """
        nums.sort()
        result = []
        path = []
        def backtrack(start):
            result.append(path[:])
            for index in range(start, len(nums)):
                if index > start and nums[index] == nums[index - 1]:
                    continue
                path.append(nums[index])
                backtrack(index + 1)
                path.pop()
        backtrack(0)
        return result
Similar exercises