← Create New Paste

Minimum Spanning Trees in Coding Interviews: Kruskal vs Prim and When Each One Wins

3 views
Minimum spanning tree questions show up less often than BFS or dynamic programming, but when they do appear they are usually disguised: connect all cities at minimum cost, lay cable between buildings, or join points on a plane with the least total distance. Recognizing that the problem is really "find an MST" is half the battle. The other half is choosing between Kruskal's and Prim's algorithm and implementing it without bugs under time pressure. What an MST is Given a connected, undirected, weighted graph, a spanning tree touches every vertex using exactly V - 1 edges and no cycles. The minimum spanning tree is the one whose edge weights sum to the smallest total. Both classic algorithms are greedy and both rely on the cut property: the cheapest edge crossing any cut between two groups of vertices is always safe to include. Kruskal's algorithm Sort all edges by weight. Walk through them in order and add an edge if its two endpoints are in different components, which you check with a disjoint set (union-find) structure using path compression and union by rank. Stop once you have V - 1 edges. The runtime is dominated by sorting: O(E log E). Kruskal shines when you are handed an explicit edge list, as in "Connecting Cities With Minimum Cost" or "Optimize Water Distribution in a Village", where a neat trick is to add a virtual node 0 connected to every house with the cost of building a well. Prim's algorithm Start from any vertex and grow the tree one vertex at a time. Keep a min-heap of edges leaving the current tree, pop the cheapest edge that reaches an unvisited vertex, add that vertex, and push its outgoing edges. With a binary heap the cost is O(E log V). Prim is often the better choice for dense graphs or implicit graphs, such as "Min Cost to Connect All Points", where every pair of points is an edge. There, a simple O(V^2) array version of Prim, without a heap, beats generating and sorting all V^2 edges. Bugs interviewers watch for - Forgetting to check whether the graph is actually connected. If you finish with fewer than V - 1 edges, return -1 or whatever the problem specifies. - Off-by-one errors when vertices are labeled from 1 instead of 0. - In Prim, marking a vertex visited when you push it instead of when you pop it, which can lock in a more expensive edge. - In Kruskal, writing union-find without path compression and then explaining an incorrect complexity. How to explain your choice Say why the greedy approach is correct (the cut property), state both complexities, and justify the one you pick based on graph density and input format. If the interviewer asks for a follow-up, be ready to discuss the second-best MST or how to update an MST when one edge weight changes. Practicing for the real thing MST problems reward a clean template you can type from memory. Practice both versions until union-find and the heap loop feel automatic. Some candidates also use Linkjob AI (https://linkjob.ai), which provides real-time help during live job interviews and technical interviews, to stay organized when explaining complexity and edge cases. Combined with steady practice, that preparation lets you spend interview time on the disguise, not the algorithm.
Created Nov 15, 2026 06:33 Expires Nov 15, 2027 06:33