PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches

Quick Overview

This question evaluates understanding of algorithms and data structures for selecting distinct top-k values and enforcing ordering constraints. It is commonly asked in coding and algorithms interviews to assess practical application skills in handling duplicates, ordering, and performance optimization, reflecting a practical algorithmic application rather than purely conceptual reasoning.

  • medium
  • Google
  • Coding & Algorithms
  • Software Engineer

Find top-k distinct elements

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Given an integer array `nums` and an integer `k`, return the **k largest distinct values** from `nums`. **Requirements** - Values in the output must be **distinct** (no duplicates). - Return the selected values in **descending order**. - If there are fewer than `k` distinct values, return **all distinct values** in descending order. **Input** - `nums`: array of integers (may contain duplicates) - `k`: positive integer **Output** - array/list of integers **Example** - `nums = [5, 2, 2, 9, 1, 9, 7], k = 3` → output `[9, 7, 5]` **Constraints (typical)** - `1 ≤ len(nums) ≤ 10^5` - `1 ≤ k ≤ len(nums)` **Follow-up** - Target time complexity better than sorting all elements when possible (e.g., `O(n log k)` using a heap).

Quick Answer: This question evaluates understanding of algorithms and data structures for selecting distinct top-k values and enforcing ordering constraints. It is commonly asked in coding and algorithms interviews to assess practical application skills in handling duplicates, ordering, and performance optimization, reflecting a practical algorithmic application rather than purely conceptual reasoning.

Given an integer array nums and an integer k, return the k largest distinct values from nums. The returned values must be unique and sorted in descending order. If nums contains fewer than k distinct values, return all distinct values in descending order. As a follow-up, aim for a solution better than sorting the entire array when possible.

Constraints

  • 1 <= len(nums) <= 10^5
  • 1 <= k <= len(nums)
  • -10^9 <= nums[i] <= 10^9

Examples

Input: ([5, 2, 2, 9, 1, 9, 7], 3)

Expected Output: [9, 7, 5]

Explanation: The distinct values are [5, 2, 9, 1, 7]. The 3 largest are 9, 7, and 5.

Input: ([4, 4, 4, 4], 2)

Expected Output: [4]

Explanation: There is only one distinct value, so return all distinct values.

Input: ([-1, -5, -3, -1, -2], 2)

Expected Output: [-1, -2]

Explanation: The distinct values are [-1, -5, -3, -2]. The 2 largest are -1 and -2.

Input: ([10], 1)

Expected Output: [10]

Explanation: A single-element array has exactly one distinct value.

Input: ([8, 6, 8, 7, 6, 5], 10)

Expected Output: [8, 7, 6, 5]

Explanation: There are only 4 distinct values, so return all of them in descending order.

Input: ([3, 1, 2, 2, 3], 3)

Expected Output: [3, 2, 1]

Explanation: The distinct values are [3, 1, 2], and all 3 should be returned in descending order.

Hints

  1. You only care about distinct values, so think about how to skip duplicates efficiently.
  2. A min-heap of size k can keep track of the current k largest distinct values without sorting everything.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ 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.

Related Coding Questions

  • Solve Flower Placement and Directory Deletion - Google (medium)
  • Compute Turnstile Crossing Times - Google (hard)
  • Simulate In-Place Cellular State Updates - Google (hard)
  • Determine Whether a Word Exists in a Graph - Google (medium)
  • Implement Checksums and Feature Rollout Evaluation - Google (medium)