Shortest Reference String Encoding a Set of Routes
Last updated: November 29, 2026
Quick Overview
Given a list of route names, find the length of the shortest string S of the form w1#w2#...# such that every route appears in S as a substring ending right before some '#'.
0
0
0 solved
Given a list of route names, find the length of the shortest string S of the form w1#w2#...# such that every route appears in S as a substring ending right before some '#'.
A Google technical screen problem that reads like a compact dictionary encoding for a Maps or Transit config. The insight being checked is that a word only needs its own slot if it is not a suffix of another word, which a reversed trie reveals as leaves.
What the Interviewer Expects
- Notice that a word that is a suffix of another can share its slot
- Build a trie of reversed words
- Sum length plus one for each leaf
- Deduplicate words
- Compare with the set-of-suffixes approach
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 output the actual encoding string and the index of each word?
- What if words can also share prefixes?
- How would you do it with a suffix-removal hash set instead?
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, lowercase strings. Output: the minimum length of a reference string as described. Example: ["time","me","bell"] gives time#bell#, length 10, since "me" is read from inside "time#"....
High-Level Design
A word a can reuse the slot of word b exactly when a is a suffix of b, because then a ends right before b's #. So the encoding needs one slot per word that is not a suffix of any other w...