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?
- A. To store the adjacency list of the graph
- B. To sort the edges by weight
- C. To detect in O(near-constant) time whether adding an edge would form a cycle
- 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
- Problem: Between Kruskal's and Prim's MST algorithms, which is typically preferred for a dense graph represented with an adjacency matrix?
- Problem: Which statement correctly contrasts 0/1 knapsack and fractional knapsack?
- Problem: Activity selection (pick the maximum number of non-overlapping intervals from a set) is solved optimally by which greedy strategy?
- Problem: Which of these coin systems is a classic counterexample where the greedy 'always take the largest coin' algorithm fails to find…
- Problem: Huffman coding builds an optimal prefix-free binary code for a set of symbol frequencies. Which greedy step does it repeat?
- Problem: What is the time complexity of computing the longest increasing subsequence using patience-sorting with binary search, versus the…