Stable Top K Frequent Words
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
With up to 200000 words in the array, any approach that rescans the list for every distinct word is dead on arrival. This Google OA, reported in November 2025, is a top K frequent words problem with a twist: ties break by first occurrence index, not alphabetically. It's a hash map plus sort or heap problem, and the tie-break is where people slip. If you've seen the classic version, don't paste it from memory. StealthCoder sits invisibly on your screen as a safety net if you blank on the details mid-assessment.
The problem
Given an array of words and an integer k, return the k distinct words with the highest frequencies. Rank words by descending frequency. When two words have the same frequency, rank the word whose first occurrence has the smaller array index first. Return the selected words in this ranking order. Function topKFrequentStable(words: String[], k: int) → String[] Examples Example 1 words = ["apple","banana","apple","cherry","banana","date"] k = 2 return = ["apple","banana"] apple and banana each occur twice. The first apple appears at index 0, before the first banana at index 1. Example 2 words = ["z","a","z","a","b"] k = 3 return = ["z","a","b"] z and a have frequency two and keep first-occurrence order. The remaining word b is third. Example 3 words = ["solo"] k = 1 return = ["solo"] The only distinct word is selected. Constraints 1 <= words.length <= 200000 1 <= words[i].length <= 30 Each word contains lowercase English letters. 1 <= k <= the number of distinct words.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Walk the array once and build a map from word to a pair: count and first index. Only set the first index when the word is new. That's O(n). Then you have two clean options. Sort the distinct entries by count descending, then first index ascending, and slice the first k. That's O(m log m) where m is distinct words. Or use a heap of size k with the same comparator, which is O(m log k). Both pass at this input size. The pitfall is copying the classic LeetCode answer, which breaks ties alphabetically. Here alphabetical order gives wrong output on example 2 style cases. Another trap is updating the first index on every occurrence, which turns it into last index. If you freeze up on the comparator during the live OA, StealthCoder is the hedge that reads the problem and hands you the working version.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Stable 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 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 Google's OA.
Google 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.
Stable Top K Frequent Words FAQ
What's the trick in Stable Top K Frequent Words?+
Store count and first-occurrence index per word in a hash map. Then rank by count descending and first index ascending. The tie-break is the whole twist. Get the comparator right and the rest is a standard frequency count.
Should I use a heap or just sort?+
Either passes with 200000 words. Sorting the distinct entries is simpler and harder to get wrong. A size-k heap is slightly faster when k is small, but you need the comparator inverted correctly. Under time pressure, sort.
How is this different from LeetCode's Top K Frequent Words?+
The classic version breaks ties alphabetically. This one breaks ties by which word appeared first in the array. If you reuse the classic comparator, you'll fail cases where the earlier word is alphabetically later, like example 2 with z and a.
What edge cases should I test?+
Single word with k equal to 1, all words identical, all words distinct with k equal to the distinct count, and ties across several words. Check that first index is only set once, on first sight, not overwritten later.
How do I prepare for this in 48 hours?+
Write the hash map plus sort version from scratch twice. Then write the size-k heap version once. Practice custom comparators in your language, since that's where mistakes happen. Know the complexity: O(n) to count, O(m log m) to sort.