Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: Between Kruskal's and Prim's MST algorithms, which is typically preferred for a dense graph represented with an adjacency matrix?

  1. A. Prim's algorithm (O(V^2) without a heap, growing one tree from a start vertex)
  2. B. Both are always identical in performance regardless of density
  3. C. Neither works on dense graphs
  4. D. Kruskal's algorithm (O(E log E), sorting all edges first)

Correct answer: A. Prim's algorithm (O(V^2) without a heap, growing one tree from a start vertex)

On a dense graph E approaches V^2, making Kruskal's O(E log E) sorting step costly; Prim's array-based O(V^2) implementation (no heap needed) avoids sorting all edges and performs better for dense graphs.

More Coding Interview Problems questions

Exams that ask this topic