Shortest Rename Chain Between Two Identifiers Changing One Letter
Last updated: November 29, 2026
Quick Overview
Given a start identifier, a target identifier and a dictionary of valid identifiers of equal length, return the number of identifiers in the shortest chain from start to target where each step changes exactly one letter and every intermediate is in the dictionary, or 0 if impossible.
Microsoft
November 29, 20260
0
0 solved
Given a start identifier, a target identifier and a dictionary of valid identifiers of equal length, return the number of identifiers in the shortest chain from start to target where each step changes exactly one letter and every intermediate is in the dictionary, or 0 if impossible.
A BFS Hard problem common in Microsoft onsites, framed as a refactoring tool that only allows one-character renames through known symbols. The interviewer checks implicit graph construction, efficient neighbor generation with wildcard buckets, and may push for bidirectional BFS.
What the Interviewer Expects
- Model identifiers as nodes with an edge for a one-letter difference
- Generate neighbors through wildcard patterns instead of comparing all pairs
- Run BFS for shortest path in an unweighted graph
- Mark visited to avoid revisiting
- Discuss bidirectional BFS for large dictionaries
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return one actual shortest chain, or all of them?
- How does bidirectional BFS change the complexity?
- What if steps can also insert or delete a letter?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: start, target (same length L), list words. The target must be in words or the answer is 0. Output: number of identifiers in the chain including both ends. If start equals target, the an...
High-Level Design
Comparing every pair of words costs O(N^2 L). Instead, bucket words by wildcard patterns: cat belongs to *at, c*t, ca*. Two words are neighbors exactly when they share a pattern. BFS from `sta...