Shortest Unique Prefix for Each PowerShell Command Name

Last updated: November 29, 2026

Quick Overview

Given a list of distinct command names, return for each one the shortest prefix that identifies it uniquely among the list, or the whole name if it is itself a prefix of another command.

Microsoft
Coding & Algorithms
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a list of distinct command names, return for each one the shortest prefix that identifies it uniquely among the list, or the whole name if it is itself a prefix of another command.

A Microsoft technical screen question tied to how PowerShell and other shells let users type abbreviated parameter or command names. The interviewer is checking that you augment a trie with pass-through counts instead of comparing every pair of strings.

What the Interviewer Expects
  • Insert all names into a trie with a count of names passing through each node
  • Stop at the first node whose count is 1
  • Fall back to the full name when no node has count 1
  • Compare to the sort-and-compare-neighbors alternative
  • State complexity in total characters
Key Topics to Cover
trie
prefix counting
strings
sorting
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
  • Solve it with sorting and comparing adjacent names instead of a trie.
  • How would you update the answers incrementally when a new command is installed?
  • How would case-insensitive matching change the trie?
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

Names are distinct, non-empty strings. For each name, return the shortest prefix that no other name in the list shares. If a name is a prefix of another (like Get-Item and Get-ItemProperty), no pr...

High-Level Design

Insert every name into a trie and increment a counter on each node the name passes through. A node's count is the number of names sharing that prefix. For each name, walk its path and stop at the firs...


Submit Your Answer
Markdown supported