Challenge - 5 Problems
Adjacency Matrix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of adjacency matrix after adding edges
What is the printed adjacency matrix after adding edges between nodes 0-1, 1-2, and 2-0 in an undirected graph with 3 nodes?
DSA Go
package main import "fmt" func main() { n := 3 matrix := make([][]int, n) for i := range matrix { matrix[i] = make([]int, n) } matrix[0][1] = 1 matrix[1][0] = 1 matrix[1][2] = 1 matrix[2][1] = 1 matrix[2][0] = 1 matrix[0][2] = 1 for i := 0; i < n; i++ { fmt.Println(matrix[i]) } }
Attempts:
2 left
💡 Hint
Remember that for an undirected graph, the adjacency matrix is symmetric.
✗ Incorrect
Edges between nodes 0-1, 1-2, and 2-0 mean matrix entries for both directions are set to 1, making the matrix symmetric with zeros on the diagonal.
❓ Predict Output
intermediateOutput of adjacency matrix for directed graph
What is the printed adjacency matrix after adding directed edges from node 0 to 1, and from node 1 to 2 in a graph with 3 nodes?
DSA Go
package main import "fmt" func main() { n := 3 matrix := make([][]int, n) for i := range matrix { matrix[i] = make([]int, n) } matrix[0][1] = 1 matrix[1][2] = 1 for i := 0; i < n; i++ { fmt.Println(matrix[i]) } }
Attempts:
2 left
💡 Hint
Directed edges only set one direction in the matrix.
✗ Incorrect
Only matrix[0][1] and matrix[1][2] are set to 1, others remain 0.
🔧 Debug
advancedIdentify the error in adjacency matrix initialization
What error will this Go code produce when initializing an adjacency matrix for 3 nodes?
package main
import "fmt"
func main() {
n := 3
matrix := make([]int, n)
for i := 0; i < n; i++ {
matrix[i] = make([]int, n)
}
fmt.Println(matrix)
}
Attempts:
2 left
💡 Hint
Check the type of 'matrix' and what make returns.
✗ Incorrect
make([]int, n) creates a slice of ints, but matrix is declared as []int, so matrix[i] = make([]int, n) tries to assign a slice to an int element causing a compile-time error.
🧠 Conceptual
advancedMemory usage of adjacency matrix vs adjacency list
For a graph with 1000 nodes and 10 edges, which statement about memory usage is true?
Attempts:
2 left
💡 Hint
Think about how adjacency matrix size depends on number of nodes, not edges.
✗ Incorrect
Adjacency matrix size is n*n (1,000,000 entries), while adjacency list stores only edges (10), so matrix uses more memory for sparse graphs.
🚀 Application
expertFind if graph is complete using adjacency matrix
Given an undirected graph with n nodes represented by an adjacency matrix, which Go function correctly checks if the graph is complete (every pair of distinct nodes connected)?
Attempts:
2 left
💡 Hint
Check all pairs except self loops must have edge 1.
✗ Incorrect
Option A checks all pairs i != j for edge presence. Option A misses checking symmetric entries for undirected graph. Option A only checks diagonal. Option A incorrectly checks diagonal too.
