Top Three Search Suggestions After Each Typed Character

Last updated: November 29, 2026

Quick Overview

Given a list of past query strings and a query being typed, return for each prefix of the typed query the up to three lexicographically smallest stored queries that start with that prefix.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a list of past query strings and a query being typed, return for each prefix of the typed query the up to three lexicographically smallest stored queries that start with that prefix.

A common Google phone screen opener framed around the Search box: as the user types, the suggestion dropdown refreshes. The interviewer checks whether you avoid rescanning the whole list per keystroke and whether you store results at trie nodes or walk efficiently.

What the Interviewer Expects
  • Build a trie over the stored queries
  • Answer each prefix by walking one node deeper instead of restarting
  • Return results in lexicographic order, at most three
  • Stop early once a prefix has no matches and return empty lists for the rest
  • State time and memory trade-offs of caching top three per node
Key Topics to Cover
trie
prefix search
lexicographic order
autocomplete
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you rank by query frequency instead of alphabetically?
  • How would you handle inserts arriving while users type?
  • What changes if k is 10 and the dictionary has a billion entries?
  • Can you solve it with sorting and binary search instead of a trie?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: queries, a list of lowercase strings, and typed, a lowercase string. Output: a list with one entry per prefix typed[:1], typed[:2], ..., each a list of at most three stored queries in s...

High-Level Design

Insert every query into a trie. At each node keep a small list top holding the three smallest words passing through that node. If we insert words in sorted order, the first three words to touch a no...


Submit Your Answer
Markdown supported