Shortest Chain of Valid SKU Codes Changing One Character at a Time
Last updated: November 29, 2026
Quick Overview
Given a start SKU code, an end SKU code and a catalog of valid codes of the same length, return the number of codes in the shortest chain from start to end where each step changes exactly one character and every intermediate code is in the catalog, or 0 if none exists.
Amazon
November 29, 20260
0
0 solved
Given a start SKU code, an end SKU code and a catalog of valid codes of the same length, return the number of codes in the shortest chain from start to end where each step changes exactly one character and every intermediate code is in the catalog, or 0 if none exists.
An Amazon onsite hard BFS question themed on migrating legacy SKU identifiers through valid intermediate codes. The interviewer checks implicit-graph BFS, an efficient neighbor generation scheme such as wildcard buckets, and a bidirectional search follow-up.
What the Interviewer Expects
- Model codes as nodes connected by one-character edits
- Avoid the O(n^2) all-pairs adjacency build
- Generate neighbors through wildcard patterns
- Use BFS for the shortest chain and count nodes, not edges
- Return 0 when the end code is not in the catalog
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
- Return every shortest chain, not only its length.
- Implement bidirectional BFS and explain when it helps.
- What if edits can also insert or delete a character?
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, end strings of length L and catalog, a list of length-L strings. Output: number of codes in the shortest chain including start and end, or 0. The start need not be in the catalog....
High-Level Design
Precompute buckets: for each catalog code and each position i, the pattern with position i replaced by * maps to the codes that match it. Two codes are neighbors exactly when they share a pattern. B...