Challenge - 5 Problems
Kruskal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Kruskal's MST edges
What is the output of the following Go code that runs Kruskal's algorithm on a graph with 4 nodes and 5 edges?
DSA Go
package main import ( "fmt" "sort" ) type Edge struct { from, to, weight int } func find(parent []int, i int) int { if parent[i] != i { parent[i] = find(parent, parent[i]) } return parent[i] } func union(parent []int, rank []int, x int, y int) { xroot := find(parent, x) yroot := find(parent, y) if rank[xroot] < rank[yroot] { parent[xroot] = yroot } else if rank[xroot] > rank[yroot] { parent[yroot] = xroot } else { parent[yroot] = xroot rank[xroot]++ } } func kruskal(edges []Edge, n int) []Edge { sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight }) parent := make([]int, n) rank := make([]int, n) for i := 0; i < n; i++ { parent[i] = i rank[i] = 0 } result := []Edge{} for _, e := range edges { x := find(parent, e.from) y := find(parent, e.to) if x != y { result = append(result, e) union(parent, rank, x, y) } } return result } func main() { edges := []Edge{{0,1,10},{0,2,6},{0,3,5},{1,3,15},{2,3,4}} mst := kruskal(edges, 4) for _, e := range mst { fmt.Printf("%d-%d:%d ", e.from, e.to, e.weight) } }
Attempts:
2 left
💡 Hint
Sort edges by weight and pick edges that don't form a cycle.
✗ Incorrect
Kruskal's algorithm sorts edges by weight and adds edges that connect different sets. The edges chosen are (2-3:4), (0-3:5), and (0-1:10).
🧠 Conceptual
intermediateUnderstanding Union-Find in Kruskal's Algorithm
In Kruskal's algorithm, what is the main purpose of the union-find data structure?
Attempts:
2 left
💡 Hint
Think about how Kruskal avoids adding edges that create loops.
✗ Incorrect
The union-find structure helps detect if two vertices belong to the same connected component, preventing cycles when adding edges.
🔧 Debug
advancedIdentify the error in this Kruskal's implementation snippet
What error will this Go code snippet cause when running Kruskal's algorithm?
DSA Go
parent := make([]int, n) for i := 0; i < n; i++ { parent[i] = 0 } // rest of union-find code omitted
Attempts:
2 left
💡 Hint
How should the parent array be initialized for union-find?
✗ Incorrect
Each node's parent should be itself initially. Setting all to 0 means all nodes appear connected to node 0, breaking cycle detection.
❓ Predict Output
advancedOutput of MST total weight calculation
What is the total weight of the MST produced by this Go code using Kruskal's algorithm?
DSA Go
package main import ( "fmt" "sort" ) type Edge struct { from, to, weight int } func find(parent []int, i int) int { if parent[i] != i { parent[i] = find(parent, parent[i]) } return parent[i] } func union(parent []int, rank []int, x int, y int) { xroot := find(parent, x) yroot := find(parent, y) if rank[xroot] < rank[yroot] { parent[xroot] = yroot } else if rank[xroot] > rank[yroot] { parent[yroot] = xroot } else { parent[yroot] = xroot rank[xroot]++ } } func kruskal(edges []Edge, n int) []Edge { sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight }) parent := make([]int, n) rank := make([]int, n) for i := 0; i < n; i++ { parent[i] = i rank[i] = 0 } result := []Edge{} for _, e := range edges { x := find(parent, e.from) y := find(parent, e.to) if x != y { result = append(result, e) union(parent, rank, x, y) } } return result } func main() { edges := []Edge{{0,1,7},{0,3,5},{1,2,8},{1,3,9},{1,4,7},{2,4,5},{3,4,15},{3,5,6},{4,5,8},{4,6,9},{5,6,11}} mst := kruskal(edges, 7) total := 0 for _, e := range mst { total += e.weight } fmt.Println(total) }
Attempts:
2 left
💡 Hint
Sum the weights of edges chosen by Kruskal's MST.
✗ Incorrect
The MST edges chosen have weights 5, 5, 6, 7, 7, and 9, totaling 39.
🚀 Application
expertKruskal's Algorithm with Edge Cases
Given a graph with 5 nodes and edges: (0-1:1), (1-2:2), (2-3:3), (3-4:4), (4-0:5), (1-3:1), what is the MST edges output by Kruskal's algorithm?
Attempts:
2 left
💡 Hint
Pick edges in ascending order without forming cycles.
✗ Incorrect
Kruskal picks edges with weights 1 (0-1), 1 (1-3), 2 (1-2), and 4 (3-4).
