Challenge - 5 Problems
BFS Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of BFS traversal on a simple graph
What is the output of the BFS traversal starting from node 'A' in the given graph?
DSA Python
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['F'],
'F': []
}
from collections import deque
def bfs(graph, start):
visited = []
queue = deque([start])
while queue:
node = queue.popleft()
if node not in visited:
visited.append(node)
queue.extend([n for n in graph[node] if n not in visited and n not in queue])
return visited
print(bfs(graph, 'A'))Attempts:
2 left
💡 Hint
Remember BFS visits neighbors level by level, left to right as they appear in adjacency list.
✗ Incorrect
BFS starts at 'A', visits neighbors 'B' and 'C' in order, then visits neighbors of 'B' ('D', 'E'), then neighbors of 'C' ('F'). The order is ['A', 'B', 'C', 'D', 'E', 'F'].
🧠 Conceptual
intermediateNumber of nodes visited in BFS
Given a graph with 7 nodes where BFS starts at node '1' and visits all reachable nodes, how many nodes will BFS visit if the graph is disconnected and only nodes 1, 2, 3 are connected?
Attempts:
2 left
💡 Hint
BFS only visits nodes reachable from the start node.
✗ Incorrect
Since only nodes 1, 2, 3 are connected and BFS starts at 1, it visits these 3 nodes only. The other nodes are disconnected and not visited.
🔧 Debug
advancedIdentify the error in BFS implementation
What error will this BFS code produce when run on a graph with cycles?
DSA Python
graph = {
'A': ['B'],
'B': ['C'],
'C': ['A']
}
from collections import deque
def bfs(graph, start):
visited = []
queue = deque([start])
while queue:
node = queue.popleft()
if node not in visited:
visited.append(node)
queue.extend(graph[node])
return visited
print(bfs(graph, 'A'))Attempts:
2 left
💡 Hint
Check how visited nodes are tracked before adding neighbors.
✗ Incorrect
The code adds neighbors without checking if they are visited, causing repeated additions and an infinite loop in cyclic graphs.
❓ Predict Output
advancedBFS traversal order with weighted graph ignoring weights
What is the BFS traversal order starting from node 1 in the weighted graph (weights ignored)?
DSA Python
graph = {
1: [2, 3],
2: [4],
3: [4, 5],
4: [6],
5: [],
6: []
}
from collections import deque
def bfs(graph, start):
visited = []
queue = deque([start])
while queue:
node = queue.popleft()
if node not in visited:
visited.append(node)
queue.extend([n for n in graph[node] if n not in visited and n not in queue])
return visited
print(bfs(graph, 1))Attempts:
2 left
💡 Hint
BFS visits neighbors in the order they appear in adjacency list.
✗ Incorrect
Starting at 1, neighbors 2 and 3 are visited in order, then neighbors of 2 (4), then neighbors of 3 (4 and 5), but 4 is already visited, so 5 is added, then neighbors of 4 (6). The order is [1, 2, 3, 4, 5, 6].
🚀 Application
expertShortest path length using BFS in unweighted graph
Given the graph below, what is the shortest path length from node 'S' to node 'G' using BFS?
DSA Python
graph = {
'S': ['A', 'B'],
'A': ['C'],
'B': ['D', 'E'],
'C': ['F'],
'D': ['G'],
'E': ['G'],
'F': [],
'G': []
}
from collections import deque
def bfs_shortest_path_length(graph, start, goal):
visited = set()
queue = deque([(start, 0)]) # (node, distance)
while queue:
node, dist = queue.popleft()
if node == goal:
return dist
if node not in visited:
visited.add(node)
for neighbor in graph[node]:
if neighbor not in visited:
queue.append((neighbor, dist + 1))
return -1
print(bfs_shortest_path_length(graph, 'S', 'G'))Attempts:
2 left
💡 Hint
BFS finds shortest path in unweighted graphs by level order traversal.
✗ Incorrect
The shortest path from 'S' to 'G' is S -> B -> D -> G or S -> B -> E -> G, both length 2 edges away from S.
