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 graph[3][3] = {0}; graph[0][1] = 1; graph[1][0] = 1; graph[1][2] = 1; graph[2][1] = 1; graph[2][0] = 1; graph[0][2] = 1; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { std::cout << graph[i][j] << " "; } std::cout << std::endl; }
Attempts:
2 left
💡 Hint
Remember that for an undirected graph, edges are symmetric in the adjacency matrix.
✗ Incorrect
Edges between nodes 0-1, 1-2, and 2-0 set the corresponding matrix entries to 1 symmetrically. So positions (0,1), (1,0), (1,2), (2,1), (2,0), and (0,2) are 1, others 0.
❓ Predict Output
intermediateOutput of adjacency matrix for directed graph
What is the adjacency matrix output after adding directed edges 0->1, 1->2, and 2->0 in a graph with 3 nodes?
DSA C++
int graph[3][3] = {0}; graph[0][1] = 1; graph[1][2] = 1; graph[2][0] = 1; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { std::cout << graph[i][j] << " "; } std::cout << std::endl; }
Attempts:
2 left
💡 Hint
Directed edges only set one direction in the matrix.
✗ Incorrect
Edges 0->1, 1->2, and 2->0 set graph[0][1], graph[1][2], and graph[2][0] to 1 respectively. Others remain 0.
🔧 Debug
advancedIdentify the error in adjacency matrix initialization
What error will this code produce when compiling or running?
int graph[3][3];
for(int i=0; i<=3; i++) {
for(int j=0; j<=3; j++) {
graph[i][j] = 0;
}
}
DSA C++
int graph[3][3]; for(int i=0; i<=3; i++) { for(int j=0; j<=3; j++) { graph[i][j] = 0; } }
Attempts:
2 left
💡 Hint
Check the loop conditions and array size carefully.
✗ Incorrect
The loops use i<=3 and j<=3 which tries to access graph[3][3], but valid indices are 0 to 2. This causes out of bounds access at runtime.
🧠 Conceptual
advancedMemory usage of adjacency matrix vs adjacency list
Which statement correctly compares memory usage of adjacency matrix and adjacency list for a graph with V vertices and E edges?
Attempts:
2 left
💡 Hint
Think about how adjacency matrix stores all possible edges.
✗ Incorrect
Adjacency matrix stores a VxV matrix, so memory is O(V^2) always. Adjacency list stores only existing edges, so memory is O(V + E).
🚀 Application
expertFind if graph is complete using adjacency matrix
Given an undirected graph with V nodes represented by an adjacency matrix, which code snippet correctly checks if the graph is complete (every pair of distinct nodes connected)?
Attempts:
2 left
💡 Hint
A complete graph has edges between every pair of different nodes.
✗ Incorrect
We must check all pairs (i,j) where i != j to ensure graph[i][j] == 1. Option A correctly does this. Option A wrongly checks diagonal too. Options A and D check diagonal incorrectly.
