Shortest Unique Prefix That Identifies 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 in the list shares, so a mention picker can disambiguate with as few keystrokes as possible.
Meta
November 29, 20260
0
0 solved
Given a list of distinct usernames, return for each one the shortest prefix that no other username in the list shares, so a mention picker can disambiguate with as few keystrokes as possible.
When tagging someone in a post or comment, Meta's mention picker narrows quickly, and this question asks for the minimum keystrokes to make each name unique. The interviewer is checking for a counting trie and a clear stopping rule.
What the Interviewer Expects
- Insert names into a trie with pass counts
- Stop at the first node where the count is 1
- Handle a name that is a prefix of another
- Return results in input order
- State complexity 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 handle names being added and removed live?
- What if names are not distinct?
- How would you compute this with sorting instead of a trie?
- How would you account for case-insensitive matching?
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: distinct lowercase usernames. Output: list of prefixes in the same order.
- The prefix for
wis the shortestw[:k]that is not a prefix of any other name. - If
wis itself a prefix of a...
High-Level Design
Insert every name into a trie where each node counts how many names pass through it. For each name, walk from the root and stop at the first node whose count is 1: only this name goes through it, so t...