PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates string-processing skills and understanding of data structures for prefix management (e.g., tries), along with algorithmic efficiency and edge-case reasoning.

  • medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Find Shortest Unique Prefixes

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given a list of distinct non-empty words, return the shortest unique prefix for each word in the same order as the input. A prefix of a word is considered unique if no other word in the list starts with that prefix. If multiple prefix lengths are possible, choose the shortest one. If no shorter prefix uniquely identifies the word, the entire word is the answer. **Example** Input: ```text ["zebra", "dog", "duck", "dove"] ``` Output: ```text ["z", "dog", "du", "dov"] ``` **Constraints** - The input contains at least one word. - All words are distinct. - Words contain only lowercase English letters. - Return the prefixes in the original input order.

Quick Answer: This question evaluates string-processing skills and understanding of data structures for prefix management (e.g., tries), along with algorithmic efficiency and edge-case reasoning.

Given a list of distinct non-empty lowercase words, return the shortest identifying prefix for each word in the same order as the input. A non-empty prefix is unique if no other word in the list starts with that prefix. If multiple prefix lengths are unique, choose the shortest one. If a word is itself a prefix of another word and therefore has no unique prefix within its own length, return the entire word.

Constraints

  • 1 <= len(words) <= 10^4
  • 1 <= len(words[i]) <= 10^3
  • The total number of characters across all words is at most 2 * 10^5
  • All words are distinct
  • Words contain only lowercase English letters

Examples

Input: (['zebra', 'dog', 'duck', 'dove'],)

Expected Output: ['z', 'dog', 'du', 'dov']

Explanation: 'z' only starts zebra. For dog, 'd' and 'do' are shared with other words, so 'dog' is needed. Duck is identified by 'du', and dove by 'dov'.

Input: (['alone'],)

Expected Output: ['a']

Explanation: With only one word, the first character is already unique.

Input: (['apple', 'ape', 'april', 'bat'],)

Expected Output: ['app', 'ape', 'apr', 'b']

Explanation: The three words starting with 'ap' need one more character to become distinguishable. 'bat' is uniquely identified by 'b'.

Input: (['app', 'apple', 'apricot', 'banana'],)

Expected Output: ['app', 'appl', 'apr', 'b']

Explanation: 'app' is a prefix of 'apple', so no shorter prefix uniquely identifies it and the full word is returned. 'apple' needs 'appl', 'apricot' needs 'apr', and 'banana' needs only 'b'.

Input: (['a', 'b', 'c'],)

Expected Output: ['a', 'b', 'c']

Explanation: Each one-character word is already uniquely identified by its only character.

Input: (['interview', 'internet', 'internal', 'interval'],)

Expected Output: ['intervi', 'interne', 'interna', 'interva']

Explanation: All words share 'inter'. The next characters still create pairs, so each word needs one more distinguishing character after that shared section.

Hints

  1. Consider building a trie where each node records how many words share the prefix represented by that node.
  2. For each word, walk through the trie until you reach the first node whose count is 1.
Last updated: Jun 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)
  • Solve a Key-Door Corridor Maze - Meta (medium)