Top K Query Suggestions
Updated
Problem statement
You are given distinct historical search queries, their positive frequencies, several requested prefixes, and an integer k.
For each prefix, return at most k queries that start with that prefix. Rank matches by decreasing frequency, breaking ties lexicographically. Matching is case-sensitive, and results for the prefixes must stay in input order.
Function
topKQuerySuggestions(queries: String[], frequencies: int[], prefixes: String[], k: int) → String[][]Examples
Example 1
queries = ["new york","news","new jersey","network","new york times"]frequencies = [50,70,40,60,30]prefixes = ["ne","new "]k = 2return = [["news","network"],["new york","new jersey"]]The queries and their frequencies are: new york fifty, news seventy, new jersey forty, network sixty, and new york times thirty. The first prefix is ne. All five queries start with it, and the two most frequent are news and network. The second prefix is new , which ends in a space. news and network do not match it. Of the rest, new york and new jersey are the two most frequent, so the answer is [["news","network"],["new york","new jersey"]].
Example 2
queries = ["cat","car"]frequencies = [5,5]prefixes = ["ca","z"]k = 3return = [["car","cat"],[]]cat and car both have a frequency of five, so they tie. Ties go in alphabetical order, so car comes before cat. Both fit within k. No query starts with z, so that prefix gets an empty list. The answer is [["car","cat"],[]].
Constraints
1 <= queries.length = frequencies.length <= 500001 <= prefixes.length <= 10001 <= queries[i].length <= 100and0 <= prefixes[i].length <= 100- Queries are distinct and contain lowercase English letters and single spaces only
1 <= frequencies[i] <= 10^91 <= k <= 20
More Microsoft Phone Screen questions
- Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
- Corporation Reporting LinesPHONE SCREEN · Seen Oct 2026
- Random Forest Classifier from ScratchPHONE SCREEN · Seen Oct 2026
- Least Recently Used CacheOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Greedy and Beam Sentence DecodingPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top K Frequent ElementsPHONE SCREEN · Seen Sep 2026
- 3SumPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Arithmetic Expression EvaluatorPHONE SCREEN · Seen Sep 2026
- Employee Hierarchy Cycle and Depth AnalysisPHONE SCREEN · Seen Sep 2026
- Most Visited String SectorsPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Remove Prefix StringsRoblox · PHONE SCREENRoblox interview questions
- Every Distinct Substring in OrderAdobe · PHONE SCREENAdobe interview questions
- Shell Path AutocompleteFigma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Find the Words for Each PrefixDocuSign · PHONE SCREENDocuSign interview questions
- Highlight and Rank Overlapping CitationsHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- Find All Word Occurrences in TextSentry · PHONE SCREENSentry interview questions
- Keyboard Autocomplete SuggestionsLyft · ONSITE INTERVIEWLyft interview questions
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Search Suggestions with a Prefix TreeCitadel · ONSITE INTERVIEWCitadel interview questions
- Suggested ProductsSquarepoint Capital · OASquarepoint Capital interview questions