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 0 in the given graph?
DSA Java
import java.util.*; class Graph { private int V; private LinkedList<Integer>[] adj; Graph(int v) { V = v; adj = new LinkedList[v]; for (int i = 0; i < v; ++i) adj[i] = new LinkedList<>(); } void addEdge(int v, int w) { adj[v].add(w); } void BFS(int s) { boolean visited[] = new boolean[V]; LinkedList<Integer> queue = new LinkedList<>(); visited[s] = true; queue.add(s); while (queue.size() != 0) { s = queue.poll(); System.out.print(s + " "); for (int n : adj[s]) { if (!visited[n]) { visited[n] = true; queue.add(n); } } } } } public class Main { public static void main(String[] args) { Graph g = new Graph(4); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(1, 2); g.addEdge(2, 0); g.addEdge(2, 3); g.addEdge(3, 3); g.BFS(0); } }
Attempts:
2 left
💡 Hint
Remember BFS visits nodes level by level using a queue.
✗ Incorrect
BFS starts at node 0, visits neighbors 1 and 2 in order, then visits neighbor 3 of node 2.
So the order is 0, 1, 2, 3.
❓ Predict Output
intermediateBFS traversal output on disconnected graph
What is the output of BFS traversal starting from node 1 in this disconnected graph?
DSA Java
import java.util.*; class Graph { private int V; private LinkedList<Integer>[] adj; Graph(int v) { V = v; adj = new LinkedList[v]; for (int i = 0; i < v; ++i) adj[i] = new LinkedList<>(); } void addEdge(int v, int w) { adj[v].add(w); } void BFS(int s) { boolean visited[] = new boolean[V]; LinkedList<Integer> queue = new LinkedList<>(); visited[s] = true; queue.add(s); while (queue.size() != 0) { s = queue.poll(); System.out.print(s + " "); for (int n : adj[s]) { if (!visited[n]) { visited[n] = true; queue.add(n); } } } } } public class Main { public static void main(String[] args) { Graph g = new Graph(5); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(3, 4); g.BFS(1); } }
Attempts:
2 left
💡 Hint
BFS only visits nodes reachable from the start node.
✗ Incorrect
Node 1 has no outgoing edges, so BFS visits only node 1.
🔧 Debug
advancedIdentify the error in BFS implementation
What error will occur when running this BFS code?
DSA Java
import java.util.*; class Graph { private int V; private LinkedList<Integer>[] adj; Graph(int v) { V = v; adj = new LinkedList[v]; for (int i = 0; i < v; ++i) adj[i] = new LinkedList<>(); } void addEdge(int v, int w) { adj[v].add(w); } void BFS(int s) { boolean visited[] = new boolean[V]; LinkedList<Integer> queue = new LinkedList<>(); visited[s] = true; queue.add(s); while (!queue.isEmpty()) { s = queue.poll(); System.out.print(s + " "); for (int n : adj[s]) { if (!visited[n]) { queue.add(n); } } } } } public class Main { public static void main(String[] args) { Graph g = new Graph(3); g.addEdge(0, 1); g.addEdge(1, 2); g.addEdge(2, 1); g.BFS(0); } }
Attempts:
2 left
💡 Hint
Check if visited nodes are marked before adding to queue.
✗ Incorrect
The code does not mark nodes as visited when adding to the queue, so nodes can be added repeatedly causing infinite loop.
🧠 Conceptual
advancedBFS traversal order on a cyclic graph
Given a cyclic graph with edges: 0->1, 1->2, 2->0, 2->3, what is the BFS traversal order starting from node 0?
Attempts:
2 left
💡 Hint
BFS visits neighbors in the order they appear in adjacency list.
✗ Incorrect
Starting at 0, neighbors are 1, then 1's neighbor 2, then 2's neighbor 3.
🚀 Application
expertNumber of nodes visited by BFS in a graph with unreachable nodes
In a graph with 6 nodes and edges: 0->1, 0->2, 1->3, 4->5, how many nodes will BFS visit starting from node 0?
Attempts:
2 left
💡 Hint
BFS visits only nodes reachable from the start node.
✗ Incorrect
From node 0, BFS visits nodes 0,1,2,3. Nodes 4 and 5 are unreachable.
