Challenge - 5 Problems
Connected Components Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of BFS Connected Components Count
What is the output of this C++ code that counts connected components using BFS?
DSA C++
/* Graph with 5 nodes and edges forming two components */ #include <iostream> #include <vector> #include <queue> using namespace std; void bfs(int start, vector<vector<int>>& adj, vector<bool>& visited) { queue<int> q; q.push(start); visited[start] = true; while (!q.empty()) { int node = q.front(); q.pop(); for (int neighbor : adj[node]) { if (!visited[neighbor]) { visited[neighbor] = true; q.push(neighbor); } } } } int main() { int n = 5; vector<vector<int>> adj(n); adj[0] = {1}; adj[1] = {0, 2}; adj[2] = {1}; adj[3] = {4}; adj[4] = {3}; vector<bool> visited(n, false); int components = 0; for (int i = 0; i < n; i++) { if (!visited[i]) { bfs(i, adj, visited); components++; } } cout << components << endl; return 0; }
Attempts:
2 left
💡 Hint
Count how many groups of nodes are connected without crossing to others.
✗ Incorrect
The graph has two connected components: nodes {0,1,2} and nodes {3,4}. BFS visits all nodes in one component before moving to the next, so the count is 2.
❓ Predict Output
intermediateOutput of BFS on Disconnected Graph
What will be printed by this code that finds connected components using BFS?
DSA C++
#include <iostream> #include <vector> #include <queue> using namespace std; int main() { int n = 6; vector<vector<int>> adj(n); adj[0] = {1}; adj[1] = {0}; adj[2] = {}; adj[3] = {4}; adj[4] = {3, 5}; adj[5] = {4}; vector<bool> visited(n, false); int count = 0; for (int i = 0; i < n; i++) { if (!visited[i]) { queue<int> q; q.push(i); visited[i] = true; while (!q.empty()) { int u = q.front(); q.pop(); for (int v : adj[u]) { if (!visited[v]) { visited[v] = true; q.push(v); } } } count++; } } cout << count << endl; return 0; }
Attempts:
2 left
💡 Hint
Remember isolated nodes count as their own component.
✗ Incorrect
The graph has three connected components: {0,1}, {2}, and {3,4,5}. BFS visits each component fully before moving on.
🔧 Debug
advancedIdentify the Bug in BFS Connected Components Code
This code aims to count connected components using BFS but gives wrong output. What is the bug?
DSA C++
#include <iostream> #include <vector> #include <queue> using namespace std; int main() { int n = 4; vector<vector<int>> adj(n); adj[0] = {1}; adj[1] = {0, 2}; adj[2] = {1}; adj[3] = {}; vector<bool> visited(n, false); int components = 0; for (int i = 0; i < n; i++) { if (!visited[i]) { queue<int> q; q.push(i); // Missing visited[i] = true here while (!q.empty()) { int node = q.front(); q.pop(); for (int neighbor : adj[node]) { if (!visited[neighbor]) { visited[neighbor] = true; q.push(neighbor); } } } components++; } } cout << components << endl; return 0; }
Attempts:
2 left
💡 Hint
Check if the first node in BFS is marked visited before processing neighbors.
✗ Incorrect
The starting node is pushed into the queue but never marked visited, so it can be re-added causing infinite loop or wrong counting.
🚀 Application
advancedFind Connected Components in a Grid Using BFS
Given a 2D grid of 1s (land) and 0s (water), how many connected components (islands) are there? Connected means horizontally or vertically adjacent.
DSA C++
#include <iostream> #include <vector> #include <queue> using namespace std; int numIslands(vector<vector<int>>& grid) { int rows = grid.size(); if (rows == 0) return 0; int cols = grid[0].size(); vector<vector<bool>> visited(rows, vector<bool>(cols, false)); int count = 0; vector<pair<int,int>> directions = {{1,0},{-1,0},{0,1},{0,-1}}; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { if (grid[r][c] == 1 && !visited[r][c]) { queue<pair<int,int>> q; q.push({r,c}); visited[r][c] = true; while (!q.empty()) { auto [x,y] = q.front(); q.pop(); for (auto [dx,dy] : directions) { int nx = x + dx, ny = y + dy; if (nx >= 0 && nx < rows && ny >= 0 && ny < cols && grid[nx][ny] == 1 && !visited[nx][ny]) { visited[nx][ny] = true; q.push({nx, ny}); } } } count++; } } } return count; } int main() { vector<vector<int>> grid = { {1,1,0,0,0}, {1,1,0,0,1}, {0,0,0,1,1}, {0,0,0,0,0}, {1,0,1,0,1} }; cout << numIslands(grid) << endl; return 0; }
Attempts:
2 left
💡 Hint
Count each group of connected 1s as one island.
✗ Incorrect
The grid has five islands: top-left block, connected top-right and middle-right block, bottom-left single, bottom-middle single, and bottom-right single.
🧠 Conceptual
expertWhy BFS is Suitable for Finding Connected Components?
Which of the following best explains why BFS is a good choice to find connected components in an undirected graph?
Attempts:
2 left
💡 Hint
Think about how BFS visits nodes in a graph.
✗ Incorrect
BFS visits all nodes connected to the start node before moving on, so it naturally groups nodes by connected components.
