Word Ladder

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

Example 1 in this Bloomberg OA, reported April 2022, turns hit into cog through hot, dot and dog, and the answer is 5 because both endpoints count. That's Word Ladder, and it's a shortest-path problem dressed up as a string problem. If you've seen it, the work is getting the edge cases right under a clock. If you haven't, the trick is short once you see it. Treat every word as a node and every one-letter change as an edge. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but you should know the shape before you start.

The problem

You are given beginWord, endWord, and a dictionary wordList. A transformation changes exactly one character, and every transformed word, including endWord, must appear in the dictionary.
Return the number of words in the shortest valid sequence from beginWord through endWord, counting both endpoints. Return 0 when no sequence exists. If the two endpoints are equal, the answer is 1 only when that word appears in the dictionary.

Function
ladderLength(beginWord: String, endWord: String, wordList: String[]) → int

Examples
Example 1
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]
return = 5
One shortest sequence is hit -> hot -> dot -> dog -> cog, which contains five words.
Example 2
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
return = 0
The endpoint is absent from the dictionary, so no valid ladder exists.
Example 3
beginWord = "same"
endWord = "same"
wordList = ["same","came"]
return = 1
The endpoints already match and the endpoint appears in the dictionary.

Constraints
1 <= beginWord.length == endWord.length <= 10.
1 <= wordList.length <= 5000.
Every dictionary word has the same length as beginWord.
Words contain only lowercase English letters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The pattern is breadth-first search on an implicit graph. Put the dictionary in a hash set for O(1) lookup. Start from beginWord with a count of 1. For each word in the queue, try swapping each position with all 26 letters. If the new word is in the set, enqueue it and remove it from the set so you never revisit it. The first time you pop endWord, return the level count. Pitfalls: endWord missing from the list means return 0 immediately. The equal-endpoints case from Example 3 returns 1 only if that word is in the dictionary. Don't forget beginWord doesn't need to be in the list. Don't use DFS, it won't give the shortest path. Cost is about 5000 words times 10 positions times 26 letters. If you freeze on the live OA, StealthCoder is the hedge that hands you this BFS template.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Word Ladder cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as word ladder. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Word Ladder FAQ

What's the trick to Word Ladder?+

Model it as a graph and run BFS from beginWord. Each word is a node, and two words connect if they differ by one letter. BFS finds the shortest path first, so the level where you reach endWord is your answer. Use a set for dictionary lookups and remove words as you visit them.

How hard is this Bloomberg question really?+

It's a medium that feels harder than it is. The BFS is standard, and most failures come from edge cases: endWord not in the list, equal endpoints, or counting steps instead of words. Get those three right and the rest is routine.

Why not DFS or backtracking?+

DFS finds a path, not the shortest one. You'd have to explore every path and compare, which blows up on a 5000-word dictionary. BFS explores by distance, so the first hit on endWord is guaranteed shortest. That's the whole reason it fits.

How do I handle the equal endpoints case?+

If beginWord equals endWord, return 1 only when that word is in wordList, otherwise 0. In Example 3, same and same with same in the list returns 1. A normal BFS that checks the dictionary for endWord up front handles this naturally if you start the count at 1.

How do I prepare for this in 48 hours?+

Write the BFS from memory twice. Cover the 26-letter swap loop, the visited-by-removal trick, and the early return of 0 when endWord is absent. Then test on the three given examples. If you want a faster version later, bidirectional BFS is the upgrade, but it's rarely needed.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it