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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone 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 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
trie
prefix counts
abbreviation
uniqueness
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 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 Problems
Sample 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 ...


Submit Your Answer
Markdown supported