← Questions

Top-k and bottom-k words in a list and in a stream

0
GoogleCodingLast asked Jan 2026

A coding interview question asked in Google's first round (coding), last asked Jan 2026. It tests heaps, hash maps and streaming.

Find the most and least frequent words, first in a fixed list, then in a stream that never ends, then for words seen only once.

Part 1: a list

  • top_bottom_k(words: list[str], k: int) -> tuple[list[str], list[str]] — the k most frequent distinct words (highest count first) and the k least frequent (lowest count first). Ties go to the alphabetically smaller word. If k exceeds the number of distinct words, return all of them.
words = ["i", "love", "leetcode", "i", "love", "coding"], k = 2
    ->  (["i", "love"], ["coding", "leetcode"])
words = ["apple", "banana", "apple", "cherry", "banana", "date"], k = 3
    ->  (["apple", "banana", "cherry"], ["cherry", "date", "apple"])

Part 2: a stream

Unless you agree on a different interface with the interviewer, assume:

  • ingest(word: str) -> None
  • top_k(k: int) -> list[str] and bottom_k(k: int) -> list[str] — the same answers over all words seen so far, with the same tie rule.
ingest("apple"); ingest("banana"); ingest("apple")
top_k(2)     ->  ["apple", "banana"]
bottom_k(2)  ->  ["banana", "apple"]

Part 3: non-repeating words

A word is non-repeating while it has been seen exactly once. Support first_k(k) and last_k(k): the first or last k currently non-repeating words by arrival, both returned in arrival order.

ingest a, b, a, b, c, d
first_k(2)   ->  ["c", "d"]
ingest c, e
last_k(2)    ->  ["d", "e"]

Constraints

  • Up to 2 * 10^5 words or operations; 0 <= k <= 2 * 10^5.
  • Queries may come before any word is ingested; they return [].

Clarifying questions

  • How are ties broken?
  • Are words case-sensitive?
  • How often are queries made compared with ingests, and is k small?

What a strong answer covers

  • Part 1: a hash map of counts, then sort distinct words by (-count, word) and (count, word), or heaps of size k.
  • Part 2: counts plus an ordered structure keyed by (count, word) (a balanced tree or sorted set) so an ingest is O(log d) and a query is O(k + log d); or bucketed counts with ordered sets per count, moving a word up one bucket per ingest. Name the cost of each choice.
  • Part 3: a doubly linked list of non-repeating words in arrival order plus a map from word to node; a word's second arrival removes its node in O(1); first_k walks from the head, last_k from the tail and reverses.
  • Tests: queries before any ingest, k = 0, k larger than the vocabulary.

Follow-ups

  • Memory is limited and the vocabulary is huge: approximate heavy hitters (Count-Min sketch, Misra-Gries).
  • Only the last hour of the stream counts.
  • Many streams on many machines: merge their counts for a global top k.

Where it's asked

  • Google · First round: coding · Last asked Jan 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI

What it tests

heaps · hash maps · streaming

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding