FastPrepStable Top K Frequent Words
Problems/Stable Top K Frequent Words

Stable Top K Frequent Words

Google logoGoogle● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks for the "top K most frequent words".

It does not give tie rules or limits. Here, words use lowercase English letters, ties follow first occurrence, and k is always positive. Streaming input and k = 0 are follow-up topics in the editorial. The example in the report uses capitalized names.

Our version is about 85% the same as the reported question.

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 = 2return = ["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 = 3return = ["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 = 1return = ["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.

More Google Phone Screen questions

  • Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
  • Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Logger Rate Limiter with Every-Arrival HistoryPHONE SCREEN · Seen Nov 2026
  • Minimum Total Layover WaitPHONE SCREEN · Seen Nov 2026
  • Reach a Destination Through Scheduled FlightsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Longest Consecutive Login StreakAmazon · ONSITE INTERVIEW
  • Group AnagramsWorkday · PHONE SCREEN
  • Time-Aware Search AutocompleteRoblox · PHONE SCREENRoblox interview questions
  • Group AnagramsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Group AnagramsEPAM · PHONE SCREENEPAM interview questions
  • Top K Frequent ElementsCapgemini · ONSITE INTERVIEWCapgemini interview questions
  • Mutual-Friend RecommendationsMeta · ONSITE INTERVIEWMeta interview questions
  • Find Duplicate File GroupsAnthropic · PHONE SCREENAnthropic interview questions
  • Top K Frequent Elements with Larger-Value Tie BreakOracle · ONSITE INTERVIEWOracle interview questions
  • Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions

Interview reports that mention this question

  • Google SWE III ML Experienced Loops: 10 Experiences (Part 3 of 4)
  • Google SWE Intern Onsite Questions: 16 Experiences (Part 2 of 3)

Google interview experiences

  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
  • Google AI Coding Interview and AI OA: 2 Experiences
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public String[] topKFrequentStable(String[] words, int k) {
        // Write your code here.
    }
}
Checking account…