Challenge - 5 Problems
BFS Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of BFS traversal on a simple graph
What is the output of the BFS traversal starting from node 0 in the given graph?
DSA Typescript
const graph = new Map<number, number[]>([ [0, [1, 2]], [1, [0, 3]], [2, [0, 4]], [3, [1]], [4, [2]] ]); function bfs(start: number, graph: Map<number, number[]>): number[] { const visited = new Set<number>(); const queue: number[] = []; const order: number[] = []; visited.add(start); queue.push(start); while (queue.length > 0) { const node = queue.shift()!; order.push(node); for (const neighbor of graph.get(node) ?? []) { if (!visited.has(neighbor)) { visited.add(neighbor); queue.push(neighbor); } } } return order; } console.log(bfs(0, graph));
Attempts:
2 left
💡 Hint
Remember BFS visits neighbors level by level, starting from the start node.
✗ Incorrect
BFS starts at node 0, visits neighbors 1 and 2 in order, then visits neighbors of 1 (which is 3), then neighbors of 2 (which is 4). So the order is 0, 1, 2, 3, 4.
🧠 Conceptual
intermediate1:30remaining
Number of nodes visited in BFS
Given a graph with 6 nodes (0 to 5) and edges: 0-1, 0-2, 1-3, 2-4, 4-5, how many nodes will BFS visit starting from node 0?
Attempts:
2 left
💡 Hint
BFS visits all nodes reachable from the start node.
✗ Incorrect
All nodes 0,1,2,3,4,5 are reachable from 0, so BFS visits all 6 nodes.
❓ Predict Output
advanced2:00remaining
BFS traversal order with disconnected graph
What is the output of BFS starting from node 0 on this graph with disconnected components?
DSA Typescript
const graph = new Map<number, number[]>([ [0, [1]], [1, [0]], [2, [3]], [3, [2]] ]); function bfs(start: number, graph: Map<number, number[]>): number[] { const visited = new Set<number>(); const queue: number[] = []; const order: number[] = []; visited.add(start); queue.push(start); while (queue.length > 0) { const node = queue.shift()!; order.push(node); for (const neighbor of graph.get(node) ?? []) { if (!visited.has(neighbor)) { visited.add(neighbor); queue.push(neighbor); } } } return order; } console.log(bfs(0, graph));
Attempts:
2 left
💡 Hint
BFS only visits nodes reachable from the start node.
✗ Incorrect
Nodes 2 and 3 are disconnected from 0, so BFS starting at 0 visits only 0 and 1.
🔧 Debug
advanced2:00remaining
Identify the error in BFS implementation
What error will this BFS code produce when run?
DSA Typescript
function bfs(start: number, graph: Map<number, number[]>): number[] {
const visited = new Set<number>();
const queue: number[] = [];
const order: number[] = [];
queue.push(start);
while (queue.length > 0) {
const node = queue.shift()!;
order.push(node);
for (const neighbor of graph.get(node) ?? []) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
queue.push(neighbor);
}
}
}
return order;
}
console.log(bfs(0, new Map([[0, [1]], [1, [0]]])));Attempts:
2 left
💡 Hint
Check if the start node is marked visited before adding to queue.
✗ Incorrect
The start node is added to the queue but never marked visited, so it can be re-added repeatedly causing an infinite loop.
🚀 Application
expert2:30remaining
Shortest path length using BFS
Given the graph below, what is the shortest path length from node 0 to node 5 using BFS?
Edges:
0-1, 0-2, 1-3, 2-4, 4-5
DSA Typescript
const graph = new Map<number, number[]>([ [0, [1, 2]], [1, [0, 3]], [2, [0, 4]], [3, [1]], [4, [2, 5]], [5, [4]] ]); function bfsShortestPath(start: number, end: number, graph: Map<number, number[]>): number { const visited = new Set<number>(); const queue: Array<{node: number; dist: number}> = []; visited.add(start); queue.push({node: start, dist: 0}); while (queue.length > 0) { const {node, dist} = queue.shift()!; if (node === end) { return dist; } for (const neighbor of graph.get(node) ?? []) { if (!visited.has(neighbor)) { visited.add(neighbor); queue.push({node: neighbor, dist: dist + 1}); } } } return -1; // if no path found } console.log(bfsShortestPath(0, 5, graph));
Attempts:
2 left
💡 Hint
Trace the shortest path step by step from 0 to 5.
✗ Incorrect
The shortest path is 0 -> 2 -> 4 -> 5, which has length 3 edges.