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"?
- A. 3
- B. 4
- C. 2
- 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
- Problem: Count the subarrays of [2, 1, 3, 2, 1, 4, 2] that have exactly 2 distinct values.
- Problem: Start at 1 and apply the rules: multiply by 3 if the number is even, add 5 if it is odd. Apply 9 steps. What is the final value?
- Problem: The stream of numbers 3, 8, 2, 9, 4, 7, 6, 1 is processed by a running median: after each number, the median of the numbers so…
- Problem: A sequence is defined by a1 = 1, a2 = 1, and an = an−1 + an−2 mod 7 for n ≥ 3. What is a30 (1-indexed)?
- Problem: A grid of size 3×4 has the cell values 1 to 12 filled row-wise. Starting at the top-left (value 1), you may move right or down…
- For a = [2, -1, 4, -3, 5], which of the following statements is TRUE? A. Some contiguous subarray sums to 7. B. Every prefix sum from the…