machine coding

Search Autocomplete

Type “ja”, see the best 10 words: a trie that keeps each prefix's top list ready.

Medium90 minutesJava · class Autocomplete

the brief

What you are building

Open any shopping app's search box and type ja. Before you finish, it suggests jacket, jam, java book…

Those suggestions are the most-searched words that start with what you typed.

You will build that engine. Words come in with a weight (how many times people searched them). For a prefix, return the best k.

must have

Requirements

  1. addWord(word, weight) adds weight to that word's total. A new word starts at 0. Weights only grow.
  2. suggest(prefix, k) returns the best k words that start with prefix.
  3. "Best" means a higher total weight first. Equal weights go in alphabetical order.
  4. A word is a prefix of itself: suggest("java", …) can return "java".
  5. The empty prefix "" matches every word. No match returns an empty list.

the contract

The Java API: class Autocomplete

public Autocomplete()
An empty dictionary.
public void addWord(String word, int weight)
word uses only a–z; weight ≥ 1.
public List<String> suggest(String prefix, int k)
Up to k words, best first. 1 ≤ k ≤ 10.
  • 1 ≤ word length ≤ 20, letters a–z only
  • 1 ≤ weight ≤ 1,000; 1 ≤ k ≤ 10
  • up to 10,000 calls
  • suggest is called far more often than addWord in real life — make it fast

example session

Calls in, answers out

Four words, then a weight update.

callreturnswhy
new Autocomplete()—
addWord("java", 5)—
addWord("javascript", 3)—
addWord("jar", 5)—
addWord("json", 2)—
suggest("ja", 2)["jar", "java"]both 5; "jar" < "java" alphabetically
suggest("j", 10)["jar", "java", "javascript", "json"]fewer than k matches
addWord("json", 4)—json is now 6
suggest("j", 2)["json", "jar"]
suggest("py", 3)[]
suggest("java", 3)["java", "javascript"]the prefix itself is a word

time plan

Your 90 minutes

  1. 10 minclarify — How are ties broken? Does adding a word twice add the weights? Is "" a valid prefix? What is the biggest k?
  2. 10 mindesign — Draw the trie for java/jar/json. Decide: search the subtree on every query, or keep a ready top list at each node?
  3. 40 mincore code — Trie insert and walk-down first. Then the simple version (DFS + heap). Run the example. Upgrade to cached lists if time allows.
  4. 20 minedge cases — Empty prefix, no match, prefix longer than every word, ties, a word that climbs after its weight grows.
  5. 10 minpolish — Name the trade-off: faster suggest costs memory and slower addWord.

Practise under real pressure: a 90-minute clock with the phases above. It keeps running if you reload the page.

your turn

Build it here

Write the whole class in the editor. Run replays the example session; Submit adds the hidden tests (edge cases and large random sessions). Stuck for 20 minutes? Open one hint at a time.

Build Autocomplete in the browser

Design: implement every operation. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    The reference, the data-structure reasoning and the rubric are hidden so you can build it first. Even a half-working attempt teaches you more than reading a perfect answer.

    Opening all three hints also shows it.

    revise

    Chapters that teach these structures