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 running the following code that adds edges to a graph with 3 nodes?
DSA Javascript
class Graph { constructor(size) { this.size = size; this.matrix = Array.from({ length: size }, () => Array(size).fill(0)); } addEdge(u, v) { this.matrix[u][v] = 1; this.matrix[v][u] = 1; } printMatrix() { for (let row of this.matrix) { console.log(row.join(' ')); } } } const g = new Graph(3); g.addEdge(0, 1); g.addEdge(1, 2); g.printMatrix();
Attempts:
2 left
💡 Hint
Remember that addEdge marks both [u][v] and [v][u] as 1 for an undirected graph.
✗ Incorrect
The graph has 3 nodes. Edges are added between 0-1 and 1-2. So matrix[0][1] and matrix[1][0] are 1, and matrix[1][2] and matrix[2][1] are 1. All other entries remain 0.
❓ Predict Output
intermediateAdjacency matrix after removing an edge
What is the output adjacency matrix after removing the edge between nodes 1 and 2 in the following code?
DSA Javascript
class Graph { constructor(size) { this.size = size; this.matrix = Array.from({ length: size }, () => Array(size).fill(0)); } addEdge(u, v) { this.matrix[u][v] = 1; this.matrix[v][u] = 1; } removeEdge(u, v) { this.matrix[u][v] = 0; this.matrix[v][u] = 0; } printMatrix() { for (let row of this.matrix) { console.log(row.join(' ')); } } } const g = new Graph(3); g.addEdge(0, 1); g.addEdge(1, 2); g.removeEdge(1, 2); g.printMatrix();
Attempts:
2 left
💡 Hint
Removing edge sets the corresponding entries to 0.
✗ Incorrect
Edges 0-1 and 1-2 were added, then edge 1-2 was removed. So only edge 0-1 remains.
🧠 Conceptual
advancedMemory usage of adjacency matrix vs adjacency list
Which statement correctly describes the memory usage difference between 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 V by V matrix, so memory is O(V^2). Adjacency list stores only existing edges, so memory is O(V + E).
❓ Predict Output
advancedOutput after checking edge existence
What is the output of the following code that checks if an edge exists between nodes 0 and 2?
DSA Javascript
class Graph { constructor(size) { this.size = size; this.matrix = Array.from({ length: size }, () => Array(size).fill(0)); } addEdge(u, v) { this.matrix[u][v] = 1; this.matrix[v][u] = 1; } hasEdge(u, v) { return this.matrix[u][v] === 1; } } const g = new Graph(3); g.addEdge(0, 1); g.addEdge(1, 2); console.log(g.hasEdge(0, 2));
Attempts:
2 left
💡 Hint
Edge 0-2 was never added.
✗ Incorrect
Edges added are 0-1 and 1-2. Edge 0-2 does not exist, so hasEdge returns false.
🧠 Conceptual
expertTime complexity of checking edge existence in adjacency matrix
What is the time complexity to check if an edge exists between two nodes in an adjacency matrix representation of a graph with V vertices?
Attempts:
2 left
💡 Hint
Adjacency matrix stores direct access to edges.
✗ Incorrect
Checking edge existence is a direct lookup in the matrix at [u][v], which is constant time O(1).
