Word Ladder Problem Statement
Given two strings, BEGIN
and END
, along with an array of strings DICT
, determine the length of the shortest transformation sequence from BEGIN
to END
. Each transformation involves changing exactly one alphabet, and the resulting word after each transformation must be present in DICT
.
Input:
Integer T representing number of test cases.
For each test case:
A string BEGIN.
A string END.
An integer N representing the length of the DICT array.
N space-separated strings denoting words in DICT.
Output:
For each test case, the output is a single integer indicating the length of the shortest transformation sequence from BEGIN to END. Output each test case result on a separate line.
Example:
# Example 1 Input:
2
hit
cog
5
hot dot dog lot log
hit
hot
2
hot cog
Output:
# Example 1 Output:
5
2
Constraints:
- 1 ≤ T ≤ 5
- 1 ≤ N ≤ 102
- 1 ≤ |S| ≤ 102
- All words have the same length.
- All transformations involve only lowercase English letters.
BEGIN
will not equalEND
.
Note:
Return -1
if no transformation sequence from BEGIN
to END
is possible. Implement the function, as printing is handled separately.

AnswerBot
4mo
The Word Ladder problem involves finding the shortest transformation sequence from one word to another by changing one letter at a time.
Use breadth-first search to find the shortest transformation seq...read more
Help your peers!
Add answer anonymously...
Tower Research Capital LLC SDE-2 interview questions & answers
A SDE-2 was asked Q. Word Ladder Problem Statement Given two strings, BEGIN and END, along with an ar...read more
A SDE-2 was asked Q. Number of Islands Problem Statement You are provided with a 2-dimensional matrix...read more
A SDE-2 was asked Q. How would you design a system like Splitwise?
Popular interview questions of SDE-2
A SDE-2 was asked Q1. Word Ladder Problem Statement Given two strings, BEGIN and END, along with an ar...read more
A SDE-2 was asked Q2. Number of Islands Problem Statement You are provided with a 2-dimensional matrix...read more
A SDE-2 was asked Q3. How would you design a system like Splitwise?
>
Tower Research Capital LLC SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

