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 Javascript
const graph = {
A: ['B', 'C'],
B: ['D', 'E'],
C: ['F'],
D: [],
E: ['F'],
F: []
};
function bfs(graph, start) {
const queue = [start];
const visited = new Set();
const result = [];
while (queue.length > 0) {
const node = queue.shift();
if (!visited.has(node)) {
visited.add(node);
result.push(node);
for (const neighbor of graph[node]) {
if (!visited.has(neighbor)) {
queue.push(neighbor);
}
}
}
}
return result;
}
console.log(bfs(graph, 'A'));Attempts:
2 left
💡 Hint
Remember BFS visits nodes level by level, starting from the start node and exploring neighbors before going deeper.
✗ 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.
❓ Predict Output
intermediateBFS traversal output with disconnected graph
What is the output of BFS traversal starting from node '1' in this graph with disconnected components?
DSA Javascript
const graph = {
1: [2],
2: [3],
3: [],
4: [5],
5: []
};
function bfs(graph, start) {
const queue = [start];
const visited = new Set();
const result = [];
while (queue.length > 0) {
const node = queue.shift();
if (!visited.has(node)) {
visited.add(node);
result.push(node);
for (const neighbor of graph[node]) {
if (!visited.has(neighbor)) {
queue.push(neighbor);
}
}
}
}
return result;
}
console.log(bfs(graph, 1));Attempts:
2 left
💡 Hint
BFS only visits nodes reachable from the start node.
✗ Incorrect
Starting from node 1, BFS visits 1, then 2, then 3. Nodes 4 and 5 are disconnected and not visited.
🔧 Debug
advancedIdentify the error in BFS implementation
What error will this BFS code produce when run?
DSA Javascript
const graph = {
A: ['B'],
B: ['C'],
C: []
};
function bfs(graph, start) {
const queue = [start];
const visited = new Set();
const result = [];
while (queue.length > 0) {
const node = queue.pop();
if (!visited.has(node)) {
visited.add(node);
result.push(node);
for (const neighbor of graph[node]) {
if (!visited.has(neighbor)) {
queue.push(neighbor);
}
}
}
}
return result;
}
console.log(bfs(graph, 'A'));Attempts:
2 left
💡 Hint
Check how queue.pop() affects the order of node processing.
✗ Incorrect
Using queue.pop() removes the last element, making the traversal behave like DFS (stack), not BFS (queue). So output is DFS order, not BFS.
🧠 Conceptual
advancedBFS traversal levels in a graph
Given the graph below, what is the list of nodes at level 2 starting BFS from node 'X'?
Graph:
X: [Y, Z]
Y: [P, Q]
Z: [R]
P: []
Q: []
R: []
Attempts:
2 left
💡 Hint
Level 0 is the start node, level 1 is its neighbors, level 2 is neighbors of neighbors.
✗ Incorrect
Level 0: X
Level 1: Y, Z
Level 2: P, Q, R
So nodes at level 2 are P, Q, and R.
🚀 Application
expertShortest path length using BFS
What is the shortest path length from node 'S' to node 'G' in the graph below using BFS?
Graph:
S: [A, B]
A: [C]
B: [D, E]
C: [F]
D: [G]
E: []
F: [G]
G: []
DSA Javascript
function bfsShortestPath(graph, start, goal) {
const queue = [{node: start, distance: 0}];
const visited = new Set();
while (queue.length > 0) {
const {node, distance} = queue.shift();
if (node === goal) {
return distance;
}
if (!visited.has(node)) {
visited.add(node);
for (const neighbor of graph[node]) {
if (!visited.has(neighbor)) {
queue.push({node: neighbor, distance: distance + 1});
}
}
}
}
return -1;
}
const graph = {
S: ['A', 'B'],
A: ['C'],
B: ['D', 'E'],
C: ['F'],
D: ['G'],
E: [],
F: ['G'],
G: []
};
console.log(bfsShortestPath(graph, 'S', 'G'));Attempts:
2 left
💡 Hint
BFS finds shortest path in unweighted graphs by exploring neighbors level by level.
✗ Incorrect
Shortest paths from S to G are S->B->D->G or S->A->C->F->G. The shortest length is 3 edges.
