Reported April 2026
Uberheap priority queue

Top-K Using a Priority Queue

Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Uber OA. Under 2s to a working solution.
Founder's read

Uber's asking for the top-K elements using a heap, reported April 2026. You've got an array, you need the K largest values, and they're explicit about the O(n log k) complexity. This is a heap-priority-queue problem disguised as a warm-up. Most candidates either brute-force sort (wrong complexity) or build a max-heap (also wrong). The trick is a min-heap of size K. StealthCoder will spot this pattern instantly if you blank under pressure.

The problem

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 Description Complete the function topKUsingPriorityQueue in the editor below. topKUsingPriorityQueue has the following parameters: Returns The two largest values are 5 and 6. Any order is acceptable.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The O(n log k) solution uses a min-heap of exactly K elements. Iterate through the array. For each number, if the heap has fewer than K elements, push it. If the heap is full and the current number is larger than the heap's minimum, pop the min and push the new number. At the end, the heap contains your top K. The key insight: you don't need to store all N elements. A sorting approach is O(n log n), which fails their constraint. A max-heap approach is also O(n log n). The min-heap keeps only K elements alive at a time, so each operation costs O(log k), not O(log n). StealthCoder will have this pattern locked before you even start typing.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Top-K Using a Priority Queue cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as top k frequent elements. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Uber's OA.

Uber reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Top-K Using a Priority Queue FAQ

Is the order of output really 'any order'?+

Yes. They said 'any order is acceptable.' Don't waste time sorting the result. Dump the heap into an array and return. That matters for your time management in the live OA.

What if K is larger than the array size?+

Return all elements. Your code should handle this gracefully: if K >= array length, just return the whole array. Most candidates miss this edge case. Check it before submitting.

Can I use a max-heap instead of a min-heap?+

Technically yes, but it's O(n log n) because you'd extract all N elements to find the largest K. That violates Uber's explicit O(n log k) requirement. Use a min-heap. That's the constraint.

Does this pattern show up in other Uber OAs?+

Yes. Top-K, K closest points, K frequent elements, merge K sorted lists. It's their favorite. If you see 'K' in the problem title, assume heap. You'll be right often enough.

How do I prepare for this in 24 hours?+

Know how to build and use a priority queue in your language. Write the min-heap solution once, trace through an example, check edge cases. That's it. You don't need to drill. Pattern recognition is faster than practice.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Uber.

OA at Uber?
Invisible during screen share
Get it