Data Structures & AlgorithmsVery hardInfosys SP/DSE · Product Company SDE

Problem: Which of these best describes why Kruskal's MST algorithm needs a Union-Find (disjoint set) structure?

  1. A. To store the adjacency list of the graph
  2. B. To sort the edges by weight
  3. C. To detect in O(near-constant) time whether adding an edge would form a cycle
  4. D. To compute vertex degrees

Correct answer: C. To detect in O(near-constant) time whether adding an edge would form a cycle

Kruskal's processes edges in increasing weight order and must skip any edge whose endpoints are already connected (which would create a cycle); Union-Find with path compression and union by rank answers that connectivity check in near-O(1) amortized time.

More Coding Interview Problems questions

Exams that ask this topic