Challenge - 5 Problems
Adjacency List Master
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 printed adjacency list after running the following code?
DSA Python
graph = {0: [], 1: [], 2: [], 3: []}
graph[0].append(1)
graph[0].append(2)
graph[1].append(2)
graph[2].append(0)
graph[2].append(3)
print(graph)Attempts:
2 left
💡 Hint
Remember that append adds elements at the end of the list in the order they are called.
✗ Incorrect
The adjacency list stores neighbors in the order they are added. The keys are integers, so printed dict keys are integers, not strings. The order of neighbors matters.
🧠 Conceptual
intermediateNumber of edges in an undirected graph adjacency list
If an undirected graph has the adjacency list below, how many edges does the graph have?
graph = {0: [1, 2], 1: [0, 2], 2: [0, 1, 3], 3: [2]}
Attempts:
2 left
💡 Hint
In an undirected graph, each edge appears twice in the adjacency list.
✗ Incorrect
Count all neighbors: 2 + 2 + 3 + 1 = 8 entries. Each edge counted twice, so edges = 8 / 2 = 4.
🔧 Debug
advancedIdentify the error in adjacency list creation
What error will this code produce when run?
nodes = 3
graph = [[]] * nodes
graph[0].append(1)
print(graph)
Attempts:
2 left
💡 Hint
Check how list multiplication affects nested lists.
✗ Incorrect
Using [[]] * nodes creates multiple references to the same list. Appending to one affects all.
❓ Predict Output
advancedOutput after removing an edge from adjacency list
What is the adjacency list after removing edge 2->3 from the graph below?
graph = {0: [1, 2], 1: [2], 2: [0, 3], 3: []}
graph[2].remove(3)
print(graph)
Attempts:
2 left
💡 Hint
The remove method deletes the first occurrence of the value.
✗ Incorrect
Removing 3 from graph[2] leaves [0]. Other lists remain unchanged.
🚀 Application
expertFind if a path exists using adjacency list
Given the adjacency list below, which option correctly returns True if there is a path from node 0 to node 3 using depth-first search (DFS)?
DSA Python
graph = {0: [1, 2], 1: [2], 2: [3], 3: []}
visited = set()
def dfs(node):
if node == 3:
return True
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
if dfs(neighbor):
return True
return False
result = dfs(0)
print(result)Attempts:
2 left
💡 Hint
DFS explores neighbors recursively and returns True when target is found.
✗ Incorrect
The DFS visits nodes 0 -> 1 -> 2 -> 3 and returns True when node 3 is reached.
