Dynamic Prefix Query Counts
Updated
Problem statement
Maintain a collection of search queries and answer how many stored query occurrences begin with a given prefix. Duplicate queries count separately.
The collection is initially empty. You are given arrays operations and values of equal length. Process entry i in order:
insert: add one occurrence of the queryvalues[i].delete: remove one occurrence of the exact queryvalues[i]. If it is absent, leave the collection unchanged.count: count all stored query occurrences that start with the prefixvalues[i].
Return only the results of the count operations, in their original order. Insertions and deletions do not produce output. An empty prefix matches every stored query occurrence. If there are no count operations, return an empty integer array.
Query strings contain only lowercase English letters and are non-empty. A count prefix uses the same alphabet and may be empty. Matching is exact and case-sensitive.
Streaming and memory discussion
The interview follow-up considers queries arriving over time, with insertions, deletions, and prefix counts interleaved. Process this finite stream without retaining a second copy of its full operation history. Explain how shared prefixes reduce repeated storage, how deletion can reclaim unused branches, and why a trie still needs memory proportional to the distinct live prefixes. A trie does not guarantee constant memory for an unlimited stream of distinct queries.
Function
processPrefixQueries(operations: String[], values: String[]) → int[]Examples
Example 1
operations = ["insert","insert","insert","count","delete","count","count"]values = ["app","apple","app","app","app","app",""]return = [3,2,2]The first prefix count includes both copies of app and apple. Deleting one app leaves two matching occurrences. The empty prefix then counts the same two stored occurrences.
Example 2
operations = ["insert","delete","count","delete","count"]values = ["apple","app","app","apple","app"]return = [1,0]app is only a prefix of the stored query, so deleting that absent exact query changes nothing. Deleting apple removes the remaining occurrence.
Example 3
operations = ["insert","delete"]values = ["x","x"]return = []Neither operation requests a count, so the returned array is empty.
Constraints
0 <= operations.length = values.length <= 2 * 10^4.- Each operation is
insert,delete, orcount. - For insertions and deletions,
1 <= values[i].length <= 100. - For counts,
0 <= values[i].length <= 100. - The sum of all value lengths is at most
2 * 10^5. - Every value contains only lowercase English letters.
- Each call starts with a new empty collection.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Longest-Match Tokenizer with Unknown SymbolsAnthropic · ONSITE INTERVIEW
- Domain Weight CalculationAmazon · ONSITE INTERVIEW
- Detect Blocked Chat PhrasesWhatNot · PHONE SCREEN · ONSITE INTERVIEW
- In-Memory Directory and File OperationsSquarepoint Capital · ONSITE INTERVIEW
- File System PathsCapital One · ONSITE INTERVIEW
- Highlight and Rank Overlapping CitationsHarvey · PHONE SCREEN · ONSITE INTERVIEW
- Extract Company Tickers from NewsSusquehanna International Group (SIG) · ONSITE INTERVIEW
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEW
- Fuzzy Word Lookup With One Misspelled LetterMeta · PHONE SCREEN