Top Three Search Suggestions for Every Typed Prefix
Last updated: November 29, 2026
Quick Overview
Given past search queries with their frequencies and a query being typed character by character, return for each prefix the three most frequent matching past queries, breaking ties alphabetically.
Flipkart
November 29, 20260
0
0 solved
Given past search queries with their frequencies and a query being typed character by character, return for each prefix the three most frequent matching past queries, breaking ties alphabetically.
Directly mirrors the Flipkart search bar, where suggestions must update on every keystroke during peak sale traffic. The interviewer checks trie construction, how you store or compute the top K per node, and the read versus write cost trade-off.
What the Interviewer Expects
- Build a trie keyed by characters
- Store a precomputed top 3 list at each node for O(1) lookup per keystroke
- Define the ranking: frequency desc, then lexicographic
- Return empty lists once the prefix matches nothing
- Compare memory and update cost against computing on demand
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you update counts in real time as new searches arrive?
- How would you support typo tolerance, like 'iphnoe'?
- How would you shard this for hundreds of millions of queries?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: dict of past query to frequency; a string being typed.
- Output: for each prefix
typed[:1],typed[:2], ..., the top 3 matching queries. - Order: higher frequency first; ties alphabetical....
High-Level Design
Insert each query into a trie. While inserting, every node on its path is a prefix of it, so offer the query to that node's top 3 list. With the lists precomputed, a keystroke is one child lookup ...