Reported September 2022
Bloombergheap priority queue

Top K Frequent Words

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

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

The tie-break rule is where this one bites: same frequency, lexicographically smaller word goes first. Bloomberg candidates reported Top K Frequent Words in September 2022, and it's a clean heap-or-sort problem with one trap. Count the words, rank by frequency descending then alphabetically ascending, return the first k. The examples are small, so they hide how the ordering fails on bigger inputs with lots of ties. If your comparator is wrong, you'll pass example 1 and fail the hidden tests. If you blank on the comparator mid-assessment, StealthCoder runs invisibly on your desktop as a safety net and gives you the working solution.

The problem

Given an array words and integer k, return the k most frequent distinct words.
Rank words by descending frequency. If two words have the same frequency, the lexicographically smaller word ranks first.

Function
topKFrequentWords(words: String[], k: int) → String[]

Examples
Example 1
words = ["i","love","leetcode","i","love","coding"]
k = 2
return = ["i","love"]
Both occur twice, and i is lexicographically smaller.
Example 2
words = ["the","day","is","sunny","the","the","the","sunny","is","is"]
k = 4
return = ["the","is","sunny","day"]
Frequencies are 4, 3, 2, and 1.

Constraints
1 <= words.length <= 10^5.
1 <= k <= number of distinct words.
Words are nonempty lowercase English strings.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Step one is a hash map of word to count. Step two is ordering. The simple route: take the distinct words and sort with a key of (-count, word), then slice the first k. That's O(n log n) in the number of distinct words and it's fine for 10^5 inputs. The heap route is O(n log k): keep a min-heap of size k, but the comparator has to be reversed. The heap's worst element is the lowest frequency, and among equal frequencies the lexicographically larger word. That reversal is the common pitfall. People write the tie-break the natural way and the heap evicts the wrong word. Also don't sort by count alone and trust stability unless you pre-sorted alphabetically. Output must be in ranked order, so pop and reverse if you use a heap. If the comparator gets tangled during the live OA, StealthCoder is the hedge that reads the problem and hands you the correct ordering.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Top K Frequent Words 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

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

⏵ The honest play

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

Bloomberg 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 Words FAQ

How hard is Top K Frequent Words really?+

Medium at most. The counting is trivial. The only real difficulty is the tie-break, since equal frequencies must be ordered alphabetically. If you can write a sort key of (-count, word), you've basically solved it. The heap version just adds a reversed comparator.

What's the trick to the tie-break?+

Sort by negative frequency first, then by the word itself ascending. In Python that's key=lambda w: (-count[w], w). With a size-k min-heap, invert the word comparison so the lexicographically larger word is evicted first among equal counts.

Should I use a heap or just sort?+

Sorting the distinct words is simpler and fast enough for 10^5 words. A heap gives O(n log k) and is the follow-up answer interviewers like. Under time pressure, write the sort first, get it passing, then mention the heap if asked.

Is this heap pattern still asked at Bloomberg?+

It was reported in September 2022, and top-k with a custom ordering is a common pattern across assessments. Expect variants: top k frequent elements, k closest points, or sorting by multiple keys. Know the counting plus ordering template cold.

How do I prepare for this in 48 hours?+

Write the counter plus sort solution from memory, then the heap version with the reversed comparator. Test it on both examples and on a case where many words tie. Check that your output order is descending by rank, not reversed.

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

OA at Bloomberg?
Invisible during screen share
Get it