Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: A connected undirected graph has 7 vertices. Its BFS tree from vertex 0 has depths: vertex 0 at depth 0, vertices 1 and 2 at depth 1, vertices 3, 4, 5 at depth 2, vertex 6 at depth 3. Every non-tree edge joins vertices whose depths differ by at most one. How many distinct BFS layers does the graph have?

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

Correct answer: A. 4

The BFS layers are the sets of vertices at each distance from the source. Depths 0, 1, 2 and 3 give four layers, so counting the edges or vertices instead answers a different question.

More Coding Interview Problems questions

Exams that ask this topic