Challenge - 5 Problems
Adjacency List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of adjacency list after adding edges
What is the printed adjacency list after running this Go code that adds edges to a graph?
DSA Go
package main import "fmt" func main() { graph := make(map[int][]int) graph[1] = append(graph[1], 2) graph[1] = append(graph[1], 3) graph[2] = append(graph[2], 4) graph[3] = append(graph[3], 4) fmt.Println(graph) }
Attempts:
2 left
💡 Hint
Remember append adds elements to the slice in order.
✗ Incorrect
The code appends 2 and 3 to graph[1], then 4 to graph[2] and graph[3]. So graph[1] has [2 3], graph[2] has [4], and graph[3] has [4].
❓ Predict Output
intermediateResulting adjacency list after removing an edge
What is the adjacency list printed after removing edge 2->4 from this graph?
DSA Go
package main import "fmt" func main() { graph := map[int][]int{ 1: {2, 3}, 2: {4}, 3: {4}, } // Remove edge 2->4 neighbors := graph[2] newNeighbors := []int{} for _, n := range neighbors { if n != 4 { newNeighbors = append(newNeighbors, n) } } graph[2] = newNeighbors fmt.Println(graph) }
Attempts:
2 left
💡 Hint
Check how the new slice excludes 4 from graph[2].
✗ Incorrect
The code removes 4 from graph[2]'s neighbors by filtering it out, so graph[2] becomes empty slice [].
🔧 Debug
advancedIdentify the error in adjacency list initialization
What error does this Go code produce when trying to add an edge to an uninitialized adjacency list?
DSA Go
package main
func main() {
var graph map[int][]int
graph[1] = append(graph[1], 2)
}Attempts:
2 left
💡 Hint
Maps must be initialized before use.
✗ Incorrect
The map graph is declared but not initialized, so writing to it causes a runtime panic.
❓ Predict Output
advancedOutput after adding duplicate edges in adjacency list
What is the adjacency list after adding duplicate edges 1->2 twice?
DSA Go
package main import "fmt" func main() { graph := make(map[int][]int) graph[1] = append(graph[1], 2) graph[1] = append(graph[1], 2) fmt.Println(graph) }
Attempts:
2 left
💡 Hint
Appending twice adds both elements.
✗ Incorrect
Appending 2 twice results in slice [2 2] for graph[1].
🧠 Conceptual
expertMemory complexity of adjacency list for sparse graph
What is the memory complexity of an adjacency list representation for a graph with V vertices and E edges?
Attempts:
2 left
💡 Hint
Adjacency list stores each vertex and its edges.
✗ Incorrect
Adjacency list stores each vertex and its edges, so memory is proportional to vertices plus edges.
