Challenge - 5 Problems
Adjacency List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of adjacency list after adding edges
What is the output of the adjacency list after running the following code?
DSA Javascript
class Graph { constructor() { this.adjList = {}; } addVertex(v) { if (!this.adjList[v]) this.adjList[v] = []; } addEdge(v, w) { this.adjList[v].push(w); this.adjList[w].push(v); } } const g = new Graph(); g.addVertex('A'); g.addVertex('B'); g.addVertex('C'); g.addEdge('A', 'B'); g.addEdge('A', 'C'); console.log(g.adjList);
Attempts:
2 left
💡 Hint
Remember that addEdge adds each vertex to the other's adjacency list.
✗ Incorrect
The addEdge method adds each vertex to the other's list, so 'A' connects to 'B' and 'C', and both 'B' and 'C' connect back to 'A'.
❓ Predict Output
intermediateResulting adjacency list after removing a vertex
What is the adjacency list after removing vertex 'B' from the graph below?
DSA Javascript
class Graph { constructor() { this.adjList = {}; } addVertex(v) { if (!this.adjList[v]) this.adjList[v] = []; } addEdge(v, w) { this.adjList[v].push(w); this.adjList[w].push(v); } removeVertex(v) { while(this.adjList[v].length) { const adjVertex = this.adjList[v].pop(); this.adjList[adjVertex] = this.adjList[adjVertex].filter(x => x !== v); } delete this.adjList[v]; } } const g = new Graph(); g.addVertex('A'); g.addVertex('B'); g.addVertex('C'); g.addEdge('A', 'B'); g.addEdge('B', 'C'); g.removeVertex('B'); console.log(g.adjList);
Attempts:
2 left
💡 Hint
Removing a vertex also removes it from other vertices' adjacency lists.
✗ Incorrect
Removing 'B' deletes it and removes 'B' from 'A' and 'C' adjacency lists, leaving 'A' and 'C' with empty arrays.
🔧 Debug
advancedIdentify the error in adjacency list edge addition
What error does the following code produce when adding an edge?
DSA Javascript
class Graph { constructor() { this.adjList = {}; } addVertex(v) { if (!this.adjList[v]) this.adjList[v] = []; } addEdge(v, w) { this.adjList[v].push(w); this.adjList[w].push(v); } } const g = new Graph(); g.addVertex('A'); g.addEdge('A', 'B');
Attempts:
2 left
💡 Hint
Check if both vertices exist before adding edges.
✗ Incorrect
Vertex 'B' was never added, so this.adjList['B'] is undefined, causing a TypeError when calling push.
❓ Predict Output
advancedOutput of adjacency list after directed edge additions
What is the adjacency list after adding directed edges as shown?
DSA Javascript
class DirectedGraph { constructor() { this.adjList = {}; } addVertex(v) { if (!this.adjList[v]) this.adjList[v] = []; } addEdge(v, w) { this.adjList[v].push(w); } } const g = new DirectedGraph(); g.addVertex('X'); g.addVertex('Y'); g.addVertex('Z'); g.addEdge('X', 'Y'); g.addEdge('Y', 'Z'); console.log(g.adjList);
Attempts:
2 left
💡 Hint
Directed edges only add one-way connections.
✗ Incorrect
Edges go from 'X' to 'Y' and 'Y' to 'Z', so adjacency lists reflect these one-way links.
🧠 Conceptual
expertNumber of edges in an undirected graph adjacency list
Given an undirected graph represented by an adjacency list where each vertex's list contains its neighbors, how do you calculate the total number of edges?
Attempts:
2 left
💡 Hint
Each edge appears twice in an undirected graph adjacency list.
✗ Incorrect
Each edge is counted twice (once for each vertex), so dividing the total sum by 2 gives the correct edge count.
