Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: A word-ladder graph: vertices are words of length 3 from {cat, cot, cog, dot, dog, hat, hot} and two words are adjacent when they differ in exactly one letter. What is the length (number of edges) of the shortest path from "cat" to "dog"?

  1. A. 3
  2. B. 4
  3. C. 2
  4. D. 5

Correct answer: A. 3

BFS over the one-letter-difference graph finds the shortest transformation. Going through 'hat' or 'hot' is not a valid route when they do not link to 'dog' in one step.

More Coding Interview Problems questions

Exams that ask this topic