Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: A directed graph on vertices 0–7 has arcs 0→1, 0→2, 1→3, 2→3, 3→4, 4→5, 2→6, 6→7. How many vertices are reachable from vertex 0 by following at most 3 arcs (vertex 0 itself counts)?

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

Correct answer: B. 7

BFS depths are 0: {0}, 1: {1, 2}, 2: {3, 6}, 3: {4, 7}, so 7 vertices are within 3 arcs; vertex 5 needs 4. Counting only the vertices exactly 3 arcs away gives 2, stopping at 2 arcs gives 5, and counting everything reachable gives 8.

More Coding Interview Problems questions

Exams that ask this topic