Shortest Unique Prefix to Abbreviate Each Username
Last updated: November 29, 2026
Quick Overview
Given a list of distinct usernames, return for each one the shortest prefix that no other username starts with, or the whole name if it is a prefix of another.
0
0
0 solved
Given a list of distinct usernames, return for each one the shortest prefix that no other username starts with, or the whole name if it is a prefix of another.
A Google phone screen question framed around @-mention autocompletion in Chat or Docs, where typing a short prefix should uniquely identify a person. The interviewer checks pass-through counts in a trie and the edge case where a name is a prefix of another.
What the Interviewer Expects
- Insert all names with pass-through counts
- Walk each name until the count drops to one
- Return the full name when no unique prefix exists
- Explain the linear time bound
- Handle a single name
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 keep abbreviations up to date as people join?
- What if names are case-insensitive but displayed with case?
- How would you compute the minimum total characters a user must type for a set of targets?
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: names, distinct lowercase strings. Output: a list of prefixes, one per name, each the shortest prefix shared by no other name. If a name is a prefix of another name (like al and alex), it...
High-Level Design
Insert all names into a trie with a count at each node of how many names pass through it. For a name, walk its path; the first node with count 1 means only this name has that prefix, so the prefix up ...