Data Structures & AlgorithmsVery hardProduct company OA
Weighted undirected graph: A–B 1, B–C 2, C–D 3, D–A 4, A–C 5, B–D 6. The MST is unique. What is the weight of the SECOND-best spanning tree (the cheapest spanning tree different from the MST)?
- A. 6
- B. 7
- C. 15
- D. 21
Correct answer: B. 7
MST: A–B 1, B–C 2, C–D 3 = 6. The cheapest different tree adds D–A 4 and drops C–D 3 from the cycle A-B-C-D: 1 + 2 + 4 = 7. 6 is the MST itself, 15 the maximum spanning tree (6 + 5 + 4) and 21 the sum of all edges.
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
- Word ladder: change one letter at a time, every intermediate word must be in the dictionary {cold, cord, card, ward, warm, word, worm…
- In this grid, how many islands of 1s are there with 4-directional connectivity, and how many with 8-directional (diagonals also connect)?…
- What is the maximum path sum in this binary tree, where a path joins any two nodes along parent–child links and need not pass through the…
- A binary trie over 4-bit numbers finds the maximum XOR of any pair from [3, 10, 5, 9, 2, 8]. What is the maximum XOR?
- In a binary tree whose root is at level 0, what is the maximum number of nodes at level 5?
- A simple directed graph (no self-loops, at most one edge from u to v) has 6 vertices. What is the maximum number of edges?