Challenge - 5 Problems
Adjacency List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of adjacency list after adding edges
What is the printed adjacency list after executing the following TypeScript code?
DSA Typescript
class Graph { adjacencyList: Map<number, number[]> = new Map(); addVertex(v: number) { if (!this.adjacencyList.has(v)) { this.adjacencyList.set(v, []); } } addEdge(v: number, w: number) { this.adjacencyList.get(v)?.push(w); this.adjacencyList.get(w)?.push(v); } printGraph() { this.adjacencyList.forEach((neighbors, vertex) => { console.log(`${vertex} -> ${neighbors.join(' -> ')} -> null`); }); } } const g = new Graph(); g.addVertex(1); g.addVertex(2); g.addVertex(3); g.addEdge(1, 2); g.addEdge(1, 3); g.printGraph();
Attempts:
2 left
💡 Hint
Remember edges are added in both directions for an undirected graph.
✗ Incorrect
The graph has vertices 1, 2, and 3. Edges connect 1 with 2 and 3. So, vertex 1's neighbors are 2 and 3. Vertices 2 and 3 each have 1 as their neighbor.
❓ Predict Output
intermediate2:00remaining
Adjacency list after removing a vertex
What is the adjacency list printed after removing vertex 2 from the graph below?
DSA Typescript
class Graph { adjacencyList: Map<number, number[]> = new Map(); addVertex(v: number) { if (!this.adjacencyList.has(v)) { this.adjacencyList.set(v, []); } } addEdge(v: number, w: number) { this.adjacencyList.get(v)?.push(w); this.adjacencyList.get(w)?.push(v); } removeVertex(v: number) { this.adjacencyList.delete(v); this.adjacencyList.forEach((neighbors) => { const index = neighbors.indexOf(v); if (index !== -1) { neighbors.splice(index, 1); } }); } printGraph() { this.adjacencyList.forEach((neighbors, vertex) => { console.log(`${vertex} -> ${neighbors.join(' -> ')} -> null`); }); } } const g = new Graph(); g.addVertex(1); g.addVertex(2); g.addVertex(3); g.addEdge(1, 2); g.addEdge(2, 3); g.addEdge(1, 3); g.removeVertex(2); g.printGraph();
Attempts:
2 left
💡 Hint
Removing a vertex also removes it from neighbors' lists.
✗ Incorrect
Vertex 2 is removed. So, it no longer appears as a vertex or neighbor. Vertices 1 and 3 remain connected.
🔧 Debug
advanced2:00remaining
Identify the error in adjacency list edge addition
What error will the following TypeScript code cause when adding an edge?
DSA Typescript
class Graph { adjacencyList: Map<number, number[]> = new Map(); addVertex(v: number) { if (!this.adjacencyList.has(v)) { this.adjacencyList.set(v, []); } } addEdge(v: number, w: number) { this.adjacencyList.get(v).push(w); this.adjacencyList.get(w).push(v); } } const g = new Graph(); g.addVertex(1); g.addEdge(1, 2);
Attempts:
2 left
💡 Hint
Check if vertices exist before adding edges.
✗ Incorrect
Vertex 2 was never added, so adjacencyList.get(2) returns undefined. Calling push on undefined causes a TypeError.
🧠 Conceptual
advanced2:00remaining
Memory efficiency of adjacency list vs adjacency matrix
Which statement correctly explains why adjacency lists are more memory efficient than adjacency matrices for sparse graphs?
Attempts:
2 left
💡 Hint
Think about how each structure stores edges.
✗ Incorrect
Adjacency lists keep only neighbors for each vertex, so they use memory proportional to edges. Adjacency matrices allocate space for all vertex pairs, even if no edge exists.
🚀 Application
expert3:00remaining
Resulting adjacency list after multiple operations
After running the following TypeScript code, what is the printed adjacency list?
DSA Typescript
class Graph { adjacencyList: Map<number, number[]> = new Map(); addVertex(v: number) { if (!this.adjacencyList.has(v)) { this.adjacencyList.set(v, []); } } addEdge(v: number, w: number) { this.adjacencyList.get(v)?.push(w); this.adjacencyList.get(w)?.push(v); } removeEdge(v: number, w: number) { this.adjacencyList.set(v, this.adjacencyList.get(v)?.filter(x => x !== w) || []); this.adjacencyList.set(w, this.adjacencyList.get(w)?.filter(x => x !== v) || []); } printGraph() { this.adjacencyList.forEach((neighbors, vertex) => { console.log(`${vertex} -> ${neighbors.join(' -> ')} -> null`); }); } } const g = new Graph(); g.addVertex(1); g.addVertex(2); g.addVertex(3); g.addVertex(4); g.addEdge(1, 2); g.addEdge(1, 3); g.addEdge(2, 4); g.addEdge(3, 4); g.removeEdge(1, 3); g.printGraph();
Attempts:
2 left
💡 Hint
Removing edge (1,3) removes 3 from 1's list and 1 from 3's list.
✗ Incorrect
Edges added: (1,2), (1,3), (2,4), (3,4). Then edge (1,3) is removed. So 1's neighbors are only 2, 3's neighbors no longer include 1.