Word Ladder
On this page
Problem statement
Given a start word, a target word and a dictionary, return the number of words in the shortest sequence that turns the start word into the target. Each step changes exactly one letter, and every word after the start must be in the dictionary. All words have the same length. If no sequence exists, return 0.
Under the word puzzle this is a shortest-path question on an unweighted graph, which is why it also shows up as "how many degrees of connection separate two members of a social network". Original problem: LeetCode 127.
Example

start = "lead", target = "gold"
dictionary = [load, goad, gold, lend, bold, road, goat]
Output: 4 # lead -> load -> goad -> gold
start = "lead", target = "gold", dictionary = [load, goad, bold]
Output: 0 # "gold" is not in the dictionary
Constraints (assumed)
- 1 ≤ word length L ≤ 10; up to 5,000 dictionary words; lowercase letters only.
- The start word does not have to be in the dictionary.
What this question tests
- Recognizing an implicit graph: words are nodes, one-letter changes are edges.
- Knowing that BFS gives the shortest path when every edge costs the same.
- Generating neighbours efficiently instead of comparing every pair of words.
Questions to ask first
- Does the count include the start word? (Here: yes, it counts words, not steps.)
- Is the start word in the dictionary? It doesn't have to be. Is the target? If not, the answer is 0.
- Are all words the same length and lowercase? (Here: yes.)
- Do you want the length or the actual sequence? Returning the path, or all shortest paths, is a common follow-up.
Try it yourself first
You have the full problem. Sketch your answer, then sign up free to read the complete walkthrough.
- The complete design, flow by flow, with pictures
- Deep dives into the hard parts
- What mid, senior and staff candidates are expected to say
- Full company question lists
Free forever. No card needed.