Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA

Problem: Vertices 0–3 with directed edges 0→1, 0→2, 1→2, 2→0, 1→3, 2→3, 3→3. How many directed walks of exactly length 3 start at vertex 0 and end at vertex 3? (A walk may repeat vertices and edges, and the self-loop 3→3 may be used.)

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

Correct answer: A. 3

Walk counts of length k are entries of the k-th power of the adjacency matrix; propagating counts step by step does the same. Counting simple paths only would exclude the walks that revisit vertices.

More Coding Interview Problems questions

Exams that ask this topic