PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency with heap/priority-queue data structures, selection algorithms, and the ability to analyze time and space complexity. It is commonly asked in the Coding & Algorithms domain to assess efficient selection method implementation and performance trade-off reasoning, testing practical application rather than purely conceptual understanding.

  • easy
  • Microsoft
  • Coding & Algorithms
  • Machine Learning Engineer

Find Top K Largest Numbers

Company: Microsoft

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

Given an unsorted array of integers `nums` and an integer `k`, return the `k` largest numbers in the array. The solution should use a heap rather than sorting the entire array. You may return the result in any order. Example: - Input: `nums = [3, 1, 5, 12, 2, 11]`, `k = 3` - Output: `[12, 11, 5]` Discuss the expected time and space complexity of your approach.

Quick Answer: This question evaluates proficiency with heap/priority-queue data structures, selection algorithms, and the ability to analyze time and space complexity. It is commonly asked in the Coding & Algorithms domain to assess efficient selection method implementation and performance trade-off reasoning, testing practical application rather than purely conceptual understanding.

Given an unsorted array of integers `nums` and an integer `k`, return the `k` largest numbers in the array using a heap-based approach. Sorting the entire array is not the intended solution. You may return the result in any order, but for deterministic testing, the reference solution returns the numbers in descending order. Example: - Input: `nums = [3, 1, 5, 12, 2, 11]`, `k = 3` - Output: `[12, 11, 5]`

Constraints

  • 0 <= len(nums) <= 100000
  • -1000000000 <= nums[i] <= 1000000000
  • 0 <= k <= len(nums)
  • Duplicates are allowed

Examples

Input: ([3, 1, 5, 12, 2, 11], 3)

Expected Output: [12, 11, 5]

Explanation: The three largest values in the array are 12, 11, and 5.

Input: ([7], 1)

Expected Output: [7]

Explanation: With only one element and k = 1, that element is the answer.

Input: ([], 0)

Expected Output: []

Explanation: An empty array with k = 0 should return an empty list.

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

Expected Output: [-1, -1]

Explanation: The two largest numbers are both -1.

Input: ([4, 4, 6, 1], 4)

Expected Output: [6, 4, 4, 1]

Explanation: When k equals the array length, return all numbers. The reference solution returns them in descending order.

Hints

  1. A min-heap of size `k` can keep track of the current `k` largest elements seen so far.
  2. When the heap already contains `k` elements, only replace the smallest one if the new number is larger.
Last updated: Apr 19, 2026

Loading coding console...

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.

Related Coding Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)