Challenge - 5 Problems
Island Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of BFS-based Number of Islands
What is the output of the following C code that counts islands using BFS on a 4x5 grid?
DSA C
#include <stdio.h> #include <stdbool.h> #include <stdlib.h> #define ROWS 4 #define COLS 5 void bfs(char grid[ROWS][COLS], bool visited[ROWS][COLS], int start_row, int start_col) { int directions[4][2] = {{-1,0},{1,0},{0,-1},{0,1}}; int queue[ROWS*COLS][2]; int front = 0, rear = 0; queue[rear][0] = start_row; queue[rear][1] = start_col; rear++; visited[start_row][start_col] = true; while(front < rear) { int r = queue[front][0]; int c = queue[front][1]; front++; for(int i=0; i<4; i++) { int nr = r + directions[i][0]; int nc = c + directions[i][1]; if(nr >= 0 && nr < ROWS && nc >= 0 && nc < COLS && grid[nr][nc] == '1' && !visited[nr][nc]) { queue[rear][0] = nr; queue[rear][1] = nc; rear++; visited[nr][nc] = true; } } } } int numIslands(char grid[ROWS][COLS]) { bool visited[ROWS][COLS] = {false}; int count = 0; for(int i=0; i<ROWS; i++) { for(int j=0; j<COLS; j++) { if(grid[i][j] == '1' && !visited[i][j]) { bfs(grid, visited, i, j); count++; } } } return count; } int main() { char grid[ROWS][COLS] = { {'1','1','0','0','0'}, {'1','1','0','0','0'}, {'0','0','1','0','0'}, {'0','0','0','1','1'} }; printf("%d\n", numIslands(grid)); return 0; }
Attempts:
2 left
💡 Hint
Count connected groups of '1's connected horizontally or vertically.
✗ Incorrect
The grid has three separate islands: top-left block, middle single cell, and bottom-right block.
❓ Predict Output
intermediate2:00remaining
Output of DFS-based Number of Islands
What is the output of this C code that counts islands using DFS on a 3x3 grid?
DSA C
#include <stdio.h> #include <stdbool.h> #define ROWS 3 #define COLS 3 void dfs(char grid[ROWS][COLS], bool visited[ROWS][COLS], int r, int c) { if(r < 0 || r >= ROWS || c < 0 || c >= COLS || grid[r][c] == '0' || visited[r][c]) return; visited[r][c] = true; dfs(grid, visited, r-1, c); dfs(grid, visited, r+1, c); dfs(grid, visited, r, c-1); dfs(grid, visited, r, c+1); } int numIslands(char grid[ROWS][COLS]) { bool visited[ROWS][COLS] = {false}; int count = 0; for(int i=0; i<ROWS; i++) { for(int j=0; j<COLS; j++) { if(grid[i][j] == '1' && !visited[i][j]) { dfs(grid, visited, i, j); count++; } } } return count; } int main() { char grid[ROWS][COLS] = { {'1','0','1'}, {'0','1','0'}, {'1','0','1'} }; printf("%d\n", numIslands(grid)); return 0; }
Attempts:
2 left
💡 Hint
Each '1' not connected horizontally or vertically counts as a separate island.
✗ Incorrect
There are five '1's isolated diagonally, so 5 islands.
🧠 Conceptual
advanced1:30remaining
Why BFS or DFS is suitable for Number of Islands?
Which reason best explains why BFS or DFS is used to solve the Number of Islands problem?
Attempts:
2 left
💡 Hint
Think about how connected parts of the grid are explored.
✗ Incorrect
BFS and DFS traverse all connected land cells to mark an entire island visited, preventing double counting.
🔧 Debug
advanced1:30remaining
Identify the bug in this DFS code snippet for Number of Islands
What error does this DFS code cause when counting islands in a grid?
DSA C
void dfs(char grid[][5], bool visited[][5], int r, int c) { if(r < 0 || r >= 5 || c < 0 || c >= 5 || grid[r][c] == '0' || visited[r][c]) return; visited[r][c] = true; dfs(grid, visited, r-1, c); dfs(grid, visited, r+1, c); dfs(grid, visited, r, c-1); dfs(grid, visited, r, c+1); }
Attempts:
2 left
💡 Hint
Check how the boundary conditions are written for row and column indices.
✗ Incorrect
The condition 'r > 4' and 'c > 4' allows r or c to be 5, which is out of bounds for 0-4 indices.
🚀 Application
expert2:30remaining
Optimizing Number of Islands for Large Grids
Which approach best optimizes counting islands in a very large grid with mostly water cells?
Attempts:
2 left
💡 Hint
Think about avoiding work on water cells and marking visited land.
✗ Incorrect
Skipping water cells and marking visited land reduces traversal and improves performance on large sparse grids.