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?
- A. 4
- B. 5
- C. 3
- 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
- Problem: A graph's vertices 0–4 are connected by edges 0-1 (1), 1-2 (1), 2-3 (1), 3-4 (1), 0-4 (5), 1-3 (3). What is the largest…
- Problem: In a directed graph on vertices 0–3, the arcs are 0→1, 1→2, 2→0, 2→3. The vertex 3 has no outgoing arcs. How many vertices can…
- Problem: A rooted tree has root 1 and children 1: [2, 3, 4], 3: [5], 4: [6, 7]. The number of nodes on the longest root-to-leaf path…
- Problem: The string "abcdcba" is checked for palindromic substrings with an odd length of at least 5. How many such substrings are there?
- Problem: What is the number of distinct strings of length 4 over the alphabet {a, b} containing at least one 'a' and at least one 'b'?
- Problem: The string "aabbccddee" is compressed by keeping each character once for every run of length at least two, followed by the count…