Daily iOS
CS fundamentals and algorithms

Trie based autocomplete

You need search autocomplete that works offline: 500,000 candidate words, and on every keystroke you return the top 10 within 16ms. How do you design the data structure?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

A Trie finds the prefix in O(length), and each node stores the top k candidates of its subtree ahead of time so a query answers in O(length + k). Updates come as a server-built snapshot that replaces the whole thing.

Share a question