Word Ladder
Problem
You are given a start word beginWord, a target word endWord, and a dictionary wordList. All words have the same length and consist of lowercase letters. A transformation sequence is a chain of words starting at beginWord and ending at endWord where each consecutive pair differs in exactly one letter, and every word after the first must be in wordList. Return the length of the shortest transformation sequence (counting both endpoints), or 0 if none exists.
- Words are at most 12 characters long, and the dictionary has at most a few thousand words.
beginWord != endWord;beginWordneed not be inwordList; all words inwordListare distinct.
Examples
beginWord = "cat", endWord = "dog", wordList = ["cot","cog","dog","dot"]→4. One shortest chain:cat → cot → cog → dog(orcat → cot → dot → dog).- Same start and end, but
wordList = ["cot","cog","dot"]→0.dogis not in the dictionary, so it can never be reached. beginWord = "x", endWord = "z", wordList = ["x","y","z"]→2.x → zdirectly, since they differ in only one letter.
Hints
- Treat each word as a node. When are two words adjacent? What kind of path are you being asked for?
- Comparing every pair of words to find neighbors costs O(N²·L). Can you generate a word's neighbors from the word itself?
- BFS over the implicit graph; generate neighbors by replacing each position with each letter and checking membership in a set (or by grouping words under wildcard patterns like
h*t).
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.