Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE
Problem: A graph on vertices 0–7 has edges 0-1, 1-2, 2-0, 3-4, 4-5, 5-6, 6-3, 6-7. The edges are removed one at a time in this order: 2-0, 6-3, 4-5. After each removal, the number of connected components is recorded. What is the sum of the component counts recorded over the three removals?
- A. 3
- B. 7
- C. 12
- D. 6
Correct answer: B. 7
Removing 2-0 and 6-3 breaks cycles, so the count stays 2 and 2; removing 4-5 then cuts the path 3-…-4 from 5-6-7, giving 3. Sum 2 + 2 + 3 = 7. Recording only the final count gives 3, assuming every removal adds a component gives 3 + 4 + 5 = 12, and missing that 4-5 is now a bridge gives 6.
More Coding Interview Problems questions
- Problem: In a graph with vertices 0–6, each edge has weight 0 or 1. Edges: 0-1 (0), 0-2 (1), 1-3 (1), 2-3 (0), 3-4 (1), 4-5 (0), 2-6 (1)…
- Problem: Vertices 0–3 with directed edges 0→1, 0→2, 1→2, 2→0, 1→3, 2→3, 3→3. How many directed walks of exactly length 3 start at vertex 0…
- Problem: A rooted tree has root 1 and children: 1→2 (weight 4), 1→3 (weight 1), 2→4 (weight 2), 2→5 (weight 7), 3→6 (weight 3). The…
- Problem: A rooted binary tree stored as a parent array over nodes 0–9: parent of node i is [-1, 0, 0, 1, 1, 2, 3, 3, 4, 5][i] (node 0 is…
- Problem: A rooted tree of 8 nodes (root 1) is given by children lists: 1: [2, 3], 2: [4, 5], 3: [6], 4: [], 5: [7, 8], 6: [], 7: [], 8…
- Problem: The house-robber rule applies on a tree: you pick a set of nodes with no parent–child pair both chosen, maximising the total…