Shortest unique abbreviation for every word
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Meta's AI-enabled coding round, last asked Feb 2026. It tests strings, tries and grouping.
Given a list of distinct words, return the shortest unique abbreviation of each one. A word is abbreviated as a prefix, then the number of characters left out, then its last character: internal with prefix i becomes i6l. Start every word with a one-letter prefix. When several words share an abbreviation, lengthen the prefix of each of them by one letter and try again, until every abbreviation is unique. If an abbreviation is not shorter than the word, keep the word itself.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
words_abbreviation(words: list[str]) -> list[str]— the result is in input order.
Example
words = ["like", "god", "internal", "me", "internet",
"interval", "intension", "face", "intrusion"]
-> ["l2e", "god", "internal", "me", "i6t",
"interval", "inte4n", "f2e", "intr4n"]
like and face do not clash (l2e and f2e). internal and interval keep clashing until their prefixes are intern and interv; by then the abbreviations (intern1l, interv1l) are no shorter than the words, so both stay whole. intension and intrusion both start as i7n, and become unique at inte4n and intr4n.
Constraints
-
1 <= len(words) <= 400;2 <= len(word) <= 400. - Words are distinct and use lowercase English letters.
Clarifying questions
- Do only words that clash get a longer prefix, or all words?
- Is the rule "not shorter than the word" or "not strictly shorter"?
- Are the words distinct and lowercase only?
- Is the output in input order?
What a strong answer covers
- The observation that only words with the same length, first letter and last letter can ever clash, so they can be grouped.
- A simple approach: keep a prefix length per word, and repeatedly lengthen it for every word whose abbreviation is shared. Worst case about
O(n^2 * L). - A faster approach per group: sort the group, compute each word's longest common prefix with its neighbours, and pick the prefix one letter longer than that. Or build a trie per group with counts. About
O(total characters * log n). - Keeping the word when the abbreviation is not shorter (words of length 3 or less always stay).
- Tests: a single word, short words, many words sharing a long prefix, words differing only in the last letter.
Follow-ups
- Check whether a given abbreviation such as
i12iz4nmatches a word. - Find the shortest abbreviation of one target word that matches no word in a dictionary.
- Words arrive one at a time and you must keep every abbreviation unique as they do.
Where it's asked
- Meta · AI-enabled coding · Last asked Feb 202660 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it
What it tests
strings · tries · grouping
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
