← Questions

Autocomplete with a trie

0
GoogleCodingLast asked Sep 2025

A coding interview question asked in Google's onsite coding round, last asked Sep 2025. It tests tries, strings and data structure design.

Build an autocomplete service on a prefix tree (trie). It stores words with an integer weight (their popularity) and returns, for a prefix, the k highest-weight words that start with it. top_k is called far more often than the dictionary changes, so extra work on updates to keep queries cheap is fine.

Interface

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

  • insert(word: str, weight: int) -> None — add a word, or overwrite its weight if it exists.
  • update(word: str, weight: int) -> None — change an existing word's weight; no-op if absent.
  • delete(word: str) -> None — remove a word; no-op if absent.
  • top_k(prefix: str, k: int) -> list[str] — the k highest-weight words with this prefix, ties broken by the smaller word; fewer if fewer match; an empty prefix matches every word.

Example

insert("cat", 5); insert("car", 3); insert("cart", 8); insert("dog", 2)
top_k("ca", 2)    ->  ["cart", "cat"]
update("car", 9)
top_k("ca", 2)    ->  ["car", "cart"]
delete("cart")
top_k("ca", 2)    ->  ["car", "cat"]
insert("cab", 5)
top_k("ca", 3)    ->  ["car", "cab", "cat"]    # cab and cat tie at 5; "cab" < "cat"
top_k("", 2)      ->  ["car", "cab"]
top_k("x", 2)     ->  []

Constraints

  • Lowercase letters a to z. Thousands to a few million words, in memory on one machine.

Clarifying questions

  • Is k bounded (say at most 10), so each node can cache a fixed-size list?
  • Should an existing word's weight be replaced or added to on insert?
  • Is the tie-break lexicographic?
  • How often do weights change compared with queries?

What a strong answer covers

  • The trie: children by letter, an end-of-word flag and weight on the node where a word ends.
  • Naive top_k: walk to the prefix node, then collect the whole subtree and select. O(P + subtree size).
  • Cached tops: each node keeps its subtree's best K words sorted by (-weight, word); top_k is O(P + k).
  • Updates: only the nodes on the word's path from root can change; rebuild each one bottom-up from its own word plus its children's cached lists. O(L · 26 · K) per update.
  • Memory cost of caching (K entries per node) and the trade-off against computing on demand; storing word ids instead of strings.
  • Tests: the dry run above, deleting a word that is a prefix of another (car and cart), empty prefix, unknown prefix.

Follow-ups

  • Tolerate a typo in the prefix (edit distance 1).
  • Serve suggestions for billions of queries across many machines (shard by prefix, precompute top lists offline).
  • Weights decay over time so recent popularity counts more.

Where it's asked

  • Google · Onsite coding · Last asked Sep 202545 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)

What it tests

tries · strings · data structure design

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