FastPrepTop-K Using a Priority Queue
Problems/Top-K Using a Priority Queue

Top-K Using a Priority Queue

Uber logoUber● EasyFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Given an integer array nums and an integer k, return the largest k elements from the array in any order.

An O(n log k) solution is expected.

Function

topKUsingPriorityQueue(nums: int[], k: int) → int[]

Complete the function topKUsingPriorityQueue in the editor below.

topKUsingPriorityQueue has the following parameters:

  1. int[] nums: the input array
  2. int k: the number of largest elements to return

Returns int[]: any ordering of the largest k values.

Examples

Example 1

nums = [3, 2, 1, 5, 6, 4]k = 2return = [5, 6]

The two largest values are 5 and 6. Any order is acceptable.

Constraints

  • 1 <= nums.length <= 2 * 10^5
  • -10^9 <= nums[i] <= 10^9
  • 1 <= k <= nums.length

More Uber problems

  • Worst-Performing Ads by CTR in a Campaign TreePHONE SCREEN · Seen Nov 2026
  • Earliest Time All Users Are ConnectedPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Max Points on a LinePHONE SCREEN · Seen Oct 2026
All Uber interview questionsSee Uber hiring insights
Saved
class Solution {
    public int[] topKUsingPriorityQueue(int[] nums, int k) {
        // write your code here
    }

}
Checking account…