Magicsheet logo

Unique Word Abbreviation

Medium
22.3%
Updated 6/1/2025

Asked by 2 Companies

Unique Word Abbreviation

What is this problem about?

The Unique Word Abbreviation interview question involves a specific way to abbreviate words: take the first letter, the number of characters in between, and the last letter (e.g., "localization" becomes "l10n"). You are given a dictionary of words and then asked to check if a new word's abbreviation is "unique" relative to that dictionary. An abbreviation is unique if no other word in the dictionary has the same abbreviation, or if only the word itself (if it's in the dictionary) has that abbreviation.

Why is this asked in interviews?

Companies like Meta and Google use the Unique Word Abbreviation coding problem to test a candidate's ability to design a data structure that handles ambiguous requirements. It’s not just about the algorithm; it’s about understanding the specific, slightly counter-intuitive rules for what makes an abbreviation "unique." It evaluates your ability to use Hash Tables for efficient lookup.

Algorithmic pattern used

The primary Array, Hash Table, Design, String interview pattern is to pre-process the dictionary into a Map. The keys are the abbreviations, and the values can be a Set of the original words that produced that abbreviation. To check if a word is unique:

  1. Generate its abbreviation.
  2. If the abbreviation is not in the Map, it's unique.
  3. If the abbreviation is in the Map, it's only unique if the Set contains exactly one word and that word is equal to the query word.

Example explanation

Dictionary: ["door", "deer", "cake"].

  1. "door" and "deer" both abbreviate to "d2r".
  2. "cake" abbreviates to "c2e". Check "cane" (c2e): Not unique, because "cake" in the dictionary already uses "c2e". Check "door" (d2r): Not unique, because "deer" also uses "d2r". Check "cart" (c2t): Unique, because "c2t" is not in the dictionary.

Common mistakes candidates make

The most frequent error is misunderstanding the "unique" rule—specifically, that if a word is in the dictionary, it doesn't "conflict" with itself. Another mistake is not handling words shorter than 3 characters correctly (though the rules usually still apply, e.g., "it" becomes "i0t" or just "it").

Interview preparation tip

When designing a system or class, always clarify the edge cases for "uniqueness" or "equality." This problem is more about logical precision and efficient mapping than complex mathematical algorithms.

Similar Questions