Reported October 2026
Amazonheap priority queue

Top K Frequent Elements

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

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

The whole question comes down to one data structure choice: a hash map to count, then a heap or buckets to pick the winners. Amazon reported this Top K Frequent Elements OA in October 2026, and the example is tiny: nums = [1,1,1,2,2,3], k = 2, return [1,2]. It looks like a warm-up, but the grader cares whether you sort everything or do better. If you've got an invite and 48 hours, learn the count-then-select shape cold. StealthCoder sits invisibly on your screen during the live OA as a safety net if your mind goes blank on the heap part.

The problem

Given a nonempty integer array nums and an integer k, return the k distinct values that occur most frequently.

Examples
Example 1
nums = [1,1,1,2,2,3]
k = 2
return = [1,2]
Value 1 appears three times and value 2 appears twice.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick has two steps. First, build a frequency map in one pass over nums. Second, pull out the k keys with the largest counts. Sorting the map entries works and costs O(n log n). The better answers are a min-heap capped at size k, which gives O(n log k), or bucket sort by frequency, which gives O(n). Bucket sort works because a count can never exceed n, so you index buckets by count and walk from the top down. Common pitfalls: using a max-heap and popping k times without thinking about cost, comparing the wrong tuple element in the heap, and forgetting that the heap must order by count, not by value. Also check what the return order should be, since the example shows [1,2] but the text only asks for the k values. If you freeze on the heap syntax in your language, StealthCoder can hand you the working version during the live OA.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Top K Frequent Elements 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

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 Amazon's OA.

Amazon reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Top K Frequent Elements FAQ

How hard is Top K Frequent Elements really?+

It's a standard medium that feels easy once you've seen it. The logic is two short phases: count with a hash map, then select the top k. Most failures come from heap syntax or comparator mistakes, not from the idea itself.

What's the trick to solving it fast?+

Count occurrences in a hash map, then keep a min-heap of size k keyed by frequency. Push each entry and pop when the heap exceeds k. What's left are your answers. Bucket sort by frequency is the O(n) alternative if you want to be faster.

Is sorting the frequency map acceptable?+

Usually it passes, since it's O(n log n) and correct. But the question is built to reward something better than a full sort. Mention or implement the heap of size k, or buckets, so your solution looks deliberate rather than lazy.

Does the output order matter for the example?+

The example returns [1,2], with 1 appearing three times and 2 appearing twice. The problem text only asks for the k most frequent values, so order isn't stated. Returning them from most to least frequent is the safest choice and matches the example.

How do I prepare for this in 48 hours?+

Write it three ways from scratch: sort the map, min-heap of size k, and bucket sort. Time yourself on each. Then test edge cases like k equal to the number of distinct values and arrays with a single element. That covers nearly every variation Amazon could throw.

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

OA at Amazon?
Invisible during screen share
Get it