Challenge - 5 Problems
Graph Terminology Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of adjacency list for directed weighted graph
What is the printed adjacency list representation of this directed weighted graph after adding edges?
DSA Go
package main import "fmt" type Edge struct { to int weight int } func main() { graph := make(map[int][]Edge) graph[1] = append(graph[1], Edge{to: 2, weight: 5}) graph[1] = append(graph[1], Edge{to: 3, weight: 3}) graph[2] = append(graph[2], Edge{to: 3, weight: 2}) graph[3] = append(graph[3], Edge{to: 1, weight: 4}) for from, edges := range graph { fmt.Printf("%d ->", from) for _, e := range edges { fmt.Printf(" (%d, %d)", e.to, e.weight) } fmt.Println() } }
Attempts:
2 left
💡 Hint
Look carefully at the order edges were added and which vertex they point to.
✗ Incorrect
The graph is directed and weighted. Edges are added in order: from 1 to 2 with weight 5, from 1 to 3 with weight 3, from 2 to 3 with weight 2, and from 3 to 1 with weight 4. The adjacency list prints edges in insertion order.
🧠 Conceptual
intermediateNumber of edges in an undirected graph
If an undirected graph has 5 vertices and each vertex is connected to every other vertex exactly once, how many edges does the graph have?
Attempts:
2 left
💡 Hint
Use the formula for edges in a complete undirected graph: n*(n-1)/2
✗ Incorrect
In an undirected graph, each edge connects two vertices. For 5 vertices fully connected, edges = 5*4/2 = 10.
❓ Predict Output
advancedOutput of BFS traversal order
What is the order of vertices visited in a BFS traversal starting from vertex 0 in this undirected graph?
DSA Go
package main import "fmt" func bfs(graph map[int][]int, start int) []int { visited := make(map[int]bool) queue := []int{start} order := []int{} for len(queue) > 0 { current := queue[0] queue = queue[1:] if !visited[current] { visited[current] = true order = append(order, current) for _, neighbor := range graph[current] { if !visited[neighbor] { queue = append(queue, neighbor) } } } } return order } func main() { graph := map[int][]int{ 0: {1, 2}, 1: {0, 3, 4}, 2: {0, 5}, 3: {1}, 4: {1, 5}, 5: {2, 4}, } order := bfs(graph, 0) fmt.Println(order) }
Attempts:
2 left
💡 Hint
BFS visits neighbors level by level in the order they appear in adjacency list.
✗ Incorrect
Starting at 0, neighbors 1 and 2 are enqueued. Visit 1, enqueue 3 and 4. Visit 2, enqueue 5. Then visit 3, 4, 5 in order.
🔧 Debug
advancedIdentify error in weighted undirected graph edge addition
What error will this code cause when adding edges to an undirected weighted graph?
DSA Go
package main import "fmt" type Edge struct { to int weight int } func main() { graph := make(map[int][]Edge) graph[1] = append(graph[1], Edge{to: 2, weight: 7}) graph[2] = append(graph[2], Edge{to: 1}) // Missing weight fmt.Println(graph) }
Attempts:
2 left
💡 Hint
Check struct initialization for all required fields.
✗ Incorrect
Edge struct requires both 'to' and 'weight' fields. Omitting 'weight' causes compilation error.
🧠 Conceptual
expertMinimum edges to connect all vertices in weighted undirected graph
In a weighted undirected graph with 8 vertices, what is the minimum number of edges required to ensure the graph is connected?
Attempts:
2 left
💡 Hint
Think about the minimum edges in a spanning tree.
✗ Incorrect
A connected graph with n vertices needs at least n-1 edges to connect all vertices without cycles.
