Top K Frequent Elements
Reported by candidates from Tennr's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
A heap, or really a frequency map plus a smart sort, carries the Tennr Top K Frequent Elements question reported in August 2025. If your OA invite is sitting there, this is the one to nail cold. It looks like the classic problem, but the tie-break rule changes the details. Equal frequencies put the smaller value first, and that's where people lose points. The pattern is heap-priority-queue, with hash-table counting underneath. StealthCoder is the safety net if your mind goes blank mid-assessment, but the idea is simple enough that you should walk in ready.
The problem
Given an integer array nums and an integer k, return the k distinct values with the highest frequencies. Order the answer by decreasing frequency. If two selected values have equal frequency, place the smaller value first. Function topKFrequent(nums: int[], k: int) → int[] Examples Example 1 nums = [1,1,1,2,2,3] k = 2 return = [1,2] 1 occurs three times and 2 occurs twice. Example 2 nums = [4,4,-1,-1,2] k = 2 return = [-1,4] The selected values tie in frequency, so the smaller value comes first. Constraints 1 <= nums.length <= 100000. -10^9 <= nums[i] <= 10^9. 1 <= k <= the number of distinct values.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Step one: count frequencies with a hash map. Step two: rank the distinct values by frequency descending, then value ascending. You can sort the distinct entries with that comparator in O(d log d), or use a min-heap of size k with the same ordering to get O(d log k). Either passes at 100000 elements. The pitfall is the tie-break. Classic solutions skip it, and Example 2 exists to catch you: -1 and 4 both appear twice, so -1 goes first. Another trap is negative values, so don't use a bucket array indexed by the number itself. Bucket by frequency instead if you want O(n), then sort within each bucket. If you freeze on the comparator during the live OA, StealthCoder runs invisibly and can hand you the working version. Test your tie case before submitting.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
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 passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as top k frequent elements. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Tennr's OA.
Tennr reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Top K Frequent Elements FAQ
How hard is Top K Frequent Elements really?+
Medium on paper, easy if you've seen it. Count with a hash map, then pick the top k. The only twist in the Tennr version is the tie-break, smaller value first on equal frequency. Get the comparator right and you're done.
What's the trick to the tie-break rule?+
Sort or heap by a pair: negative frequency first, then the value itself. In a sort, that's key (-count, value). With a min-heap of size k, the 'worst' element is lowest frequency, and on a tie the larger value, so evict that one.
Should I use a heap or just sort?+
Sorting the distinct values is simpler and fast enough here. Distinct count is at most 100000, so O(d log d) is fine. A size-k heap is better when k is small and you want to show the optimization. Pick whichever you can write without bugs.
Is this pattern still asked in 2025?+
Yes. Tennr had it reported in August 2025. Frequency counting with top-k selection is a staple, and variants keep showing up with small rule changes like the tie-break here. Know the base version and how to adapt the ordering.
How do I prepare in 48 hours?+
Write this one from scratch twice, once with sort and once with a heap. Then test on negatives, all-equal frequencies, and k equal to the distinct count. Check that Example 2 returns [-1,4]. That covers nearly every failure mode.