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)?
- A. 2
- B. 7
- C. 5
- 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
- Problem: A 4×4 grid has obstacles at (1,1), (1,2), (2,1) (row, column, 0-indexed). Movement is up, down, left or right. How many distinct…
- Problem: An undirected graph on 9 vertices (0–8) has edges 0-1, 1-2, 3-4, 4-5, 5-3, 6-7. Vertex 8 is isolated. Count the unordered pairs…
- Problem: A rooted tree (root 1) has children lists: 1: [2, 3, 4], 2: [5, 6], 3: [], 4: [7], 5: [], 6: [8, 9], 7: [], 8: [], 9: []. How…
- Problem: A tree has nodes 1–6 with weighted edges 1-2 (3), 1-3 (5), 2-4 (4), 2-5 (2), 3-6 (7). What is the weighted diameter (maximum over…
- Problem: A rooted tree (root 1) with children: 1: [2, 3], 2: [4, 5], 3: [6], 4: [7], 5: [], 6: [], 7: []. Children are visited in…
- Problem: Insert the words "car", "cat", "care", "dog", "do", "dot" into an empty trie (one node per character, root has no letter). How…