Challenge - 5 Problems
Island Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateOutput of BFS-based Number of Islands
What is the output of the following C++ code that counts islands using BFS?
DSA C++
int numIslands(vector<vector<char>>& grid) { int count = 0; int rows = grid.size(); int cols = grid[0].size(); vector<vector<bool>> visited(rows, vector<bool>(cols, false)); vector<pair<int,int>> directions = {{1,0},{-1,0},{0,1},{0,-1}}; for(int i=0; i<rows; i++) { for(int j=0; j<cols; j++) { if(grid[i][j] == '1' && !visited[i][j]) { count++; queue<pair<int,int>> q; q.push({i,j}); visited[i][j] = true; while(!q.empty()) { auto [r,c] = q.front(); q.pop(); for(auto& d : directions) { int nr = r + d.first; int nc = c + d.second; if(nr>=0 && nr<rows && nc>=0 && nc<cols && grid[nr][nc]=='1' && !visited[nr][nc]) { q.push({nr,nc}); visited[nr][nc] = true; } } } } } } return count; } int main() { vector<vector<char>> grid = { {'1','1','0','0','0'}, {'1','1','0','0','0'}, {'0','0','1','0','0'}, {'0','0','0','1','1'} }; cout << numIslands(grid) << endl; return 0; }
Attempts:
2 left
π‘ Hint
Count connected groups of '1's using BFS.
β Incorrect
The grid has three separate islands: top-left block, middle single cell, and bottom-right block.
β Predict Output
intermediateOutput of DFS-based Number of Islands
What is the output of this C++ code that counts islands using DFS?
DSA C++
void dfs(int r, int c, vector<vector<char>>& grid, vector<vector<bool>>& visited) { int rows = grid.size(); int cols = grid[0].size(); if(r<0 || c<0 || r>=rows || c>=cols || grid[r][c]=='0' || visited[r][c]) return; visited[r][c] = true; dfs(r+1, c, grid, visited); dfs(r-1, c, grid, visited); dfs(r, c+1, grid, visited); dfs(r, c-1, grid, visited); } int numIslands(vector<vector<char>>& grid) { int count = 0; int rows = grid.size(); int cols = grid[0].size(); vector<vector<bool>> visited(rows, vector<bool>(cols, false)); for(int i=0; i<rows; i++) { for(int j=0; j<cols; j++) { if(grid[i][j]=='1' && !visited[i][j]) { count++; dfs(i,j,grid,visited); } } } return count; } int main() { vector<vector<char>> grid = { {'1','1','0','0','0'}, {'1','0','0','1','1'}, {'0','0','1','0','0'}, {'1','0','0','1','1'} }; cout << numIslands(grid) << endl; return 0; }
Attempts:
2 left
π‘ Hint
DFS explores all connected land cells recursively.
β Incorrect
There are five islands: top-left block, top-right block, middle single cell, bottom-left single cell, bottom-right block.
π§ Debug
advancedIdentify the error in BFS island counting code
What error will this code cause when counting islands using BFS?
DSA C++
int numIslands(vector<vector<char>>& grid) { int count = 0; int rows = grid.size(); int cols = grid[0].size(); vector<vector<bool>> visited(rows, vector<bool>(cols, false)); vector<pair<int,int>> directions = {{1,0},{-1,0},{0,1},{0,-1}}; for(int i=0; i<=rows; i++) { for(int j=0; j<cols; j++) { if(grid[i][j] == '1' && !visited[i][j]) { count++; queue<pair<int,int>> q; q.push({i,j}); visited[i][j] = true; while(!q.empty()) { auto [r,c] = q.front(); q.pop(); for(auto& d : directions) { int nr = r + d.first; int nc = c + d.second; if(nr>=0 && nr<rows && nc>=0 && nc<cols && grid[nr][nc]=='1' && !visited[nr][nc]) { q.push({nr,nc}); visited[nr][nc] = true; } } } } } } return count; }
Attempts:
2 left
π‘ Hint
Check loop boundary conditions carefully.
β Incorrect
The outer loop uses i <= rows which causes i to go out of bounds, leading to runtime error.
π§ Conceptual
advancedWhy use BFS or DFS for Number of Islands?
Why are BFS and DFS suitable algorithms to count the number of islands in a grid?
Attempts:
2 left
π‘ Hint
Think about how connected components are found in graphs.
β Incorrect
BFS and DFS traverse all connected nodes (land cells) starting from one cell, marking them visited to count each island once.
π Application
expertModify DFS to count islands diagonally connected
Given the DFS code for counting islands, which modification will correctly count islands connected diagonally as well?
DSA C++
void dfs(int r, int c, vector<vector<char>>& grid, vector<vector<bool>>& visited) { int rows = grid.size(); int cols = grid[0].size(); if(r<0 || c<0 || r>=rows || c>=cols || grid[r][c]=='0' || visited[r][c]) return; visited[r][c] = true; dfs(r+1, c, grid, visited); dfs(r-1, c, grid, visited); dfs(r, c+1, grid, visited); dfs(r, c-1, grid, visited); } // rest of code unchanged
Attempts:
2 left
π‘ Hint
Diagonal neighbors are cells at rowΒ±1 and colΒ±1.
β Incorrect
To count diagonal connections, DFS must explore all 8 neighbors, including diagonals.
