Data Structures & AlgorithmsModerateTCS NQT · Accenture pattern
An undirected graph has edges A–B, A–C, B–D, C–D, C–E, D–F, E–G, F–G, G–H. How many vertices are at a shortest distance of EXACTLY 2 edges from A?
- A. 3
- B. 2
- C. 4
- D. 1
Correct answer: B. 2
BFS from A: level 1 = B, C; level 2 = D (from B or C) and E (from C); level 3 = F, G; level 4 = H. So exactly 2 vertices (D and E). Counting D twice, once via B and once via C, gives 3 – BFS marks a vertex when it is first discovered.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A graph has 9 vertices and 3 connected components. How many edges does a minimum spanning FOREST (an MST of every component) contain?
- Kahn's algorithm uses a MIN-heap (smallest available vertex first) on the DAG with edges 3→1, 3→4, 2→4, 4→0, 1→0, 5→2. What topological…
- The words bat, bath, bad, ban, band are inserted into an empty trie (one node per character, plus the root). How many nodes does the trie…
- Keys 50, 30, 70, 20, 40, 60, 80, 35, 65, 10 are inserted into an empty BST. How many nodes have EXACTLY one child?
- A climber can take 1 or 3 steps at a time. In how many different ordered ways can a staircase of 7 steps be climbed? (ways(n) = ways(n −…
- Keys 50, 40, 30, 45, 47 are inserted in that order into an empty AVL tree. What is the preorder traversal of the final tree?