Find Top K Largest Numbers
Company: Microsoft
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
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.
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
- A min-heap of size `k` can keep track of the current `k` largest elements seen so far.