Total Prefix Popularity Score for Every Query in a Log
Last updated: November 29, 2026
Quick Overview
For each string in a list, compute the sum over all its non-empty prefixes of how many strings in the list start with that prefix.
0
0
0 solved
For each string in a list, compute the sum over all its non-empty prefixes of how many strings in the list start with that prefix.
A Google online assessment style task framed around scoring queries by how widely their prefixes are shared, a signal autocomplete teams care about. It checks storing pass-through counts at trie nodes and summing along a path.
What the Interviewer Expects
- Insert all strings with a pass count at each node
- Sum counts along each string's path
- Recognize that the answer counts the string itself
- Avoid the quadratic per-pair prefix comparison
- State time as linear in total characters
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 scores when a new query is added?
- What if you only count prefixes of length at least 3?
- How would you rank queries by score with ties broken by frequency?
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: words, a list of lowercase strings (duplicates allowed). Output: a list where entry i is the sum, over every non-empty prefix p of words[i], of the number of words in the list that star...
High-Level Design
Two passes over a trie. First insert every word and increment a counter c at every node passed, so a node's counter equals the number of words having that node's string as a prefix. Second, for each...