Word Ladder
Updated
Problem statement
Given two distinct lowercase words beginWord and endWord, and an array wordList, return the number of words in the shortest transformation sequence from beginWord to endWord.
Each transformation changes exactly one letter, and every transformed word after beginWord must appear in wordList. Return 0 when no valid sequence exists.
Function
ladderLength(beginWord: String, endWord: String, wordList: List<String>) → intExamples
Example 1
beginWord = "hit"endWord = "cog"wordList = ["hot","dot","dog","lot","log","cog"]return = 5One shortest sequence is hit → hot → dot → dog → cog, which contains 5 words.
Example 2
beginWord = "hit"endWord = "cog"wordList = ["hot","dot","dog","lot","log"]return = 0The destination cog is unavailable, so no valid sequence can reach it.
Example 3
beginWord = "lost"endWord = "cost"wordList = ["most","fist","lost","cost","fish"]return = 2Changing the first letter transforms lost directly into cost.
Constraints
1 <= beginWord.length == endWord.length <= 10.beginWordandendWordare distinct lowercase English words.1 <= wordList.length <= 5000.- Every entry in
wordListis a unique lowercase word with the same length asbeginWord. beginWorddoes not need to appear inwordList.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Find Duplicate Files in a FilesystemHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
- Word LadderGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Word LadderMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Word Ladder IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Shortest Word Transformation SequenceReddit · PHONE SCREENReddit interview questions
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Find Minimum City HopsAmazon · OA
- Shortest Path from Encoded Graph EdgesBitGo · OABitGo interview questions
- Minimum Bus Routes to a DestinationPinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Jump Game IVZomato / Eternal · ONSITE INTERVIEWZomato / Eternal interview questions