PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Google

Generate all subsets (handle duplicates)

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of power-set generation and handling of duplicate elements, measuring competency in combinatorics, enumeration techniques and algorithmic problem-solving.

  • medium
  • Google
  • Coding & Algorithms
  • Software Engineer

Generate all subsets (handle duplicates)

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem Given an integer array `nums`, return **all possible subsets** (the power set). ### Part A (no duplicates) Assume all elements in `nums` are **distinct**. Return all subsets in any order. ### Part B (follow-up: may contain duplicates) Now assume `nums` **may contain duplicate values**. Return all **unique** subsets (no duplicate subsets). Subsets can be returned in any order. ## Input - `nums`: an array of integers, length `n` ## Output - A list of lists, where each inner list is a subset of `nums` ## Constraints - `0 <= n <= 20` - `-10 <= nums[i] <= 10` ## Examples ### Example 1 (Part A) Input: `nums = [1,2,3]` Output (one valid ordering): `[[],[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]]` ### Example 2 (Part B) Input: `nums = [1,2,2]` Output (one valid ordering): `[[],[1],[2],[1,2],[2,2],[1,2,2]]` ## Clarifications to ask - Are elements guaranteed distinct, or can there be duplicates? - If duplicates exist, should the output contain only unique subsets?

Quick Answer: This question evaluates understanding of power-set generation and handling of duplicate elements, measuring competency in combinatorics, enumeration techniques and algorithmic problem-solving.

Related Interview Questions

  • Solve Rooms and Top-K Streams - Google (medium)
  • Find Containing Range - Google (medium)
  • Rearrange Tasks With Cooldown - Google (medium)
  • Implement Employee Management and Expression Evaluation - Google (medium)
  • Solve Three Array and Matrix Path Problems - Google (medium)
Google logo
Google
Jan 4, 2026, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
7
0
Loading...

Problem

Given an integer array nums, return all possible subsets (the power set).

Part A (no duplicates)

Assume all elements in nums are distinct. Return all subsets in any order.

Part B (follow-up: may contain duplicates)

Now assume nums may contain duplicate values. Return all unique subsets (no duplicate subsets). Subsets can be returned in any order.

Input

  • nums : an array of integers, length n

Output

  • A list of lists, where each inner list is a subset of nums

Constraints

  • 0 <= n <= 20
  • -10 <= nums[i] <= 10

Examples

Example 1 (Part A)

Input: nums = [1,2,3] Output (one valid ordering): [[],[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]]

Example 2 (Part B)

Input: nums = [1,2,2] Output (one valid ordering): [[],[1],[2],[1,2],[2,2],[1,2,2]]

Clarifications to ask

  • Are elements guaranteed distinct, or can there be duplicates?
  • If duplicates exist, should the output contain only unique subsets?

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Google•More Software Engineer•Google Software Engineer•Google Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.