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 C#
int n = 3; int[,] matrix = new int[n, 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 (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write(matrix[i, j] + " "); } Console.WriteLine(); }
Attempts:
2 left
💡 Hint
Remember that for an undirected graph, the adjacency matrix is symmetric.
✗ Incorrect
Edges 0-1, 1-2, and 2-0 are added symmetrically, so matrix[i,j] and matrix[j,i] are set to 1 for these pairs. The diagonal remains 0 because no self-loops are added.
❓ Predict Output
intermediateValue in adjacency matrix after removing an edge
Given a directed graph with 3 nodes and edges 0->1, 1->2, 2->0, what is the value of matrix[1,2] after removing the edge 1->2?
DSA C#
int n = 3; int[,] matrix = new int[n, n]; matrix[0, 1] = 1; matrix[1, 2] = 1; matrix[2, 0] = 1; matrix[1, 2] = 0; // remove edge Console.WriteLine(matrix[1, 2]);
Attempts:
2 left
💡 Hint
Removing an edge sets the corresponding matrix cell to 0.
✗ Incorrect
The edge 1->2 is removed by setting matrix[1,2] to 0, so printing matrix[1,2] outputs 0.
🔧 Debug
advancedIdentify the error in adjacency matrix initialization
What error will this C# code produce when trying to create an adjacency matrix for 3 nodes?
int[,] matrix = new int[3];
DSA C#
int[,] matrix = new int[3];
Attempts:
2 left
💡 Hint
Check the syntax for declaring a 2D array in C#.
✗ Incorrect
The code declares a 2D array but provides a 1D array initializer. The correct syntax requires two dimensions, e.g., new int[3,3]. The compiler error CS0029 indicates cannot implicitly convert type 'int[]' to 'int[,]'.
❓ Predict Output
advancedOutput after checking for edge existence
What is the output of the following code that checks if an edge exists from node 0 to node 2 in a directed graph?
DSA C#
int[,] matrix = new int[3, 3]; matrix[0, 1] = 1; matrix[1, 2] = 1; if (matrix[0, 2] == 1) { Console.WriteLine("Edge exists"); } else { Console.WriteLine("No edge"); }
Attempts:
2 left
💡 Hint
Check if matrix[0,2] was set to 1 or not.
✗ Incorrect
Only edges 0->1 and 1->2 are set. matrix[0,2] remains 0, so the else branch runs printing 'No edge'.
🧠 Conceptual
expertMemory usage comparison of adjacency matrix vs adjacency list
For a graph with 1000 nodes and 10 edges, which statement about memory usage is correct?
Attempts:
2 left
💡 Hint
Think about how adjacency matrix stores all possible edges.
✗ Incorrect
Adjacency matrix stores a 1000x1000 matrix (1,000,000 entries), mostly zeros, while adjacency list stores only actual edges (10 entries). So matrix uses much more memory.
