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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

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
trie
prefix counts
string processing
typeahead
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: distinct lowercase usernames. Output: list of prefixes in the same order.
  • The prefix for w is the shortest w[:k] that is not a prefix of any other name.
  • If w is 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...


Submit Your Answer
Markdown supported