Product Search Autocomplete Returning the Top Three by Popularity
Last updated: November 29, 2026
Quick Overview
Given product names with popularity scores, build a structure that returns the three most popular products starting with a typed prefix, breaking ties alphabetically. Also return the suggestion lists for each character as the shopper types a query.
Walmart Labs
November 29, 20260
0
0 solved
Given product names with popularity scores, build a structure that returns the three most popular products starting with a typed prefix, breaking ties alphabetically. Also return the suggestion lists for each character as the shopper types a query.
Search typeahead on Walmart.com and the Walmart app has to answer on every keystroke. In the Walmart Labs onsite the interviewer checks that you choose a trie, precompute the top results per node so queries do not scan subtrees, and reason about memory.
What the Interviewer Expects
- Choose a trie keyed by characters
- Precompute the top k names at each node
- Insert in ranked order to keep node lists trivially correct
- Return an empty list for unknown prefixes
- Discuss memory cost and how updates change the design
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 a popularity score without rebuilding the trie?
- How would you handle typos such as `paprr` for paper?
- The catalog has 400 million items; how do you shard or compress the trie?
- How would you personalize suggestions per shopper?
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: a list of (name, popularity). Query: a prefix, matched case-insensitively. Output: up to three names, sorted by popularity descending then name ascending. An unknown prefix returns [], and ...
High-Level Design
A trie where each node stores a short list of the best names passing through it. If we insert products in ranked order (highest popularity first, alphabetical on ties), then the first three names to t...