Design Search Autocomplete System
Updated
Problem statement
You are given three arrays:
queries[i]is a search query issued by a user.timestamps[i]is the time whenqueries[i]was issued.prefixes[j]is a prefix typed into an autocomplete box.
For each prefix, return every distinct query that starts with that prefix. Each query string shows up only once in a list, even if it was issued many times. Rank the queries like this:
- The query issued more times comes first.
- If the counts tie, compare earliest timestamps. A query's earliest timestamp is the earliest time it was ever issued. The smaller one comes first.
- If both still tie, the lexicographically smaller query comes first. That means the one that comes first in dictionary order, comparing characters one by one.
Return one ranked list for each prefix, in the same order as prefixes. If no query starts with a prefix, return an empty list for that prefix. The empty prefix matches every distinct query.
This is the batch version of the classic search autocomplete design question. One way to solve it is a trie (a prefix tree) built from the query strings. The node where a query ends stores that query's count and earliest timestamp. For each prefix, walk down to its node, collect every query below it, and sort them by the three rules above. In a live interview, ask whether the answer should hold all matching queries or only the top k suggestions.
Function
autocomplete(queries: String[], timestamps: int[], prefixes: String[]) → List<List<String>>Examples
Example 1
queries = ["roblox studio", "roblox", "roadblocks", "roblox studio", "roblox game", "roblox"]timestamps = [5, 1, 7, 3, 4, 2]prefixes = ["rob", "robl", "road", "x"]return = [["roblox", "roblox studio", "roblox game"], ["roblox", "roblox studio", "roblox game"], ["roadblocks"], []]"roblox" and "roblox studio" each appear twice, so they rank ahead of "roblox game", which appears once. The two queries with count 2 tie, so compare their earliest timestamps: 1 for "roblox" and 3 for "roblox studio". So "roblox" comes first. The prefixes "rob" and "robl" match the same three queries. "road" matches only "roadblocks", and "x" matches nothing, so its list is empty.
Example 2
queries = ["alpha", "alpine", "alpha", "alpine"]timestamps = [10, 3, 2, 8]prefixes = ["al"]return = [["alpha", "alpine"]]Both queries appear twice, so the counts tie. Next compare earliest timestamps: "alpha" was first issued at time 2 and "alpine" at time 3. So "alpha" ranks first.
Example 3
queries = ["aba", "abb", "aba", "abb"]timestamps = [1, 1, 5, 7]prefixes = ["ab"]return = [["aba", "abb"]]Both queries appear twice, and both have earliest timestamp 1. Everything ties, so dictionary order decides: "aba" comes before "abb".
Constraints
queries.length == timestamps.length0 <= queries.length <= 10^51 <= prefixes.length <= 10^41 <= queries[i].length <= 1000 <= prefixes[i].length <= 1000 <= timestamps[i] <= 10^9queries[i]contains printable ASCII characters.prefixes[i]is empty or contains printable ASCII characters.
More Bloomberg LP Onsite Interview questions
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Replacements to Make Two Strings AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Design Search Autocomplete SystemRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Trie and Heap Autocomplete SystemCitadel · PHONE SCREENCitadel interview questions
- Design a Food Rating SystemApple · PHONE SCREENApple interview questions
- Word Dictionary with Wildcard FindAmazon · ONSITE INTERVIEW
- Design File SystemGoogle · PHONE SCREENGoogle interview questions
- Implement a TrieMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Add Word and Suffix SearchAnduril · ONSITE INTERVIEWAnduril interview questions
- Trie: Add and Search WordsAdobe · ONSITE INTERVIEWAdobe interview questions
- In-Memory Directory and File OperationsSquarepoint Capital · ONSITE INTERVIEWSquarepoint Capital interview questions
- Implement TrieOptiver · PHONE SCREENOptiver interview questions
Interview reports that mention this question
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)