Top K Largest Values with a Heap
Reported by candidates from Meta's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Meta reportedly put this one in front of candidates in September 2026, and under the wrapper it's just "keep the biggest k numbers, then sort them." The problem even hands you the method: a min-heap capped at size k. If your OA invite is sitting there with a clock on it, this is a good one to see. The logic is short, and the traps are small and avoidable. Duplicates count separately, k can be zero, and the output must be in nonincreasing order. If you freeze mid-assessment, StealthCoder runs invisibly as a safety net and hands you the solution in real time. Know the heap trick first and you probably won't need it.
The problem
Given an integer array nums and an integer k, return the k largest occurrences in nonincreasing order. Preserve duplicate occurrences. Process the array with a min-heap containing at most k values, then order the selected values for the returned result. Return an empty array when k is zero. Function topKLargest(nums: int[], k: int) → int[] Examples Example 1 nums = [3,2,1,5,6,4] k = 2 return = [6,5] The size-two heap retains 5 and 6. Example 2 nums = [4,1,4,2,4] k = 3 return = [4,4,4] Duplicate occurrences remain separate selected values. Example 3 nums = [-5,-1,-3] k = 0 return = [] Selecting zero values returns an empty result. Constraints 0 <= nums.length <= 200000. -10^9 <= nums[i] <= 10^9. 0 <= k <= nums.length.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The pattern is a bounded min-heap. Push each number. When the heap grows past k, pop the smallest. What's left after one pass are the k largest values, and the heap root is the weakest of them. That's O(n log k) time and O(k) space, which matters with up to 200000 elements. Then pop everything out or sort descending to get the required order. Pitfalls: forgetting k = 0 (return empty before touching the heap), deduplicating values (Example 2 wants [4,4,4]), and returning heap order, which isn't sorted. Popping a min-heap gives ascending output, so reverse it. Don't sort the whole array if the prompt asks for a heap, since the follow-up question is often about complexity. Values reach 10^9 in magnitude, so negatives are fine with standard comparisons. If you blank on the language's heap API during the live OA, StealthCoder is the hedge that gets you unstuck quickly.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Top K Largest Values with a Heap 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Meta's OA.
Meta 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 Largest Values with a Heap FAQ
What's the trick in the Meta top K largest values problem?+
Use a min-heap capped at size k. Push each number, and if the size exceeds k, pop the smallest. The survivors are the k largest. Then reverse or sort them into nonincreasing order. That's O(n log k), better than sorting everything when k is small.
How hard is this problem really?+
Easy to medium. The algorithm is standard and the prompt basically spells it out. Difficulty comes from edge cases: k equals zero, duplicates kept as separate values, and output ordering. Get those three right and it's a short, clean solution.
Do I need to remove duplicates?+
No. Example 2 returns [4,4,4] for k = 3, so each occurrence counts on its own. Don't use a set. Just push every element into the heap and let the size cap handle selection.
Why does the output need extra sorting after the heap?+
A min-heap only guarantees the smallest element is at the root, not full order. Popping gives ascending values, so you reverse them for nonincreasing output. Or collect the heap contents and sort descending. Skipping this step is the most common wrong answer.
How do I prepare for this in 48 hours?+
Write the capped min-heap solution from memory in your OA language twice. Learn its heap push and pop calls, since syntax is where people stall. Test k = 0, an empty array, all negatives, and heavy duplicates. That covers nearly every failure case.