Challenge - 5 Problems
Island Mastery
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#
using System; using System.Collections.Generic; class Solution { public int NumIslands(char[][] grid) { if (grid == null || grid.Length == 0) return 0; int count = 0; int rows = grid.Length; int cols = grid[0].Length; bool[][] visited = new bool[rows][]; for (int i = 0; i < rows; i++) visited[i] = new bool[cols]; int[][] directions = new int[][] { new int[] {1, 0}, new int[] {-1, 0}, new int[] {0, 1}, new int[] {0, -1} }; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { if (grid[r][c] == '1' && !visited[r][c]) { count++; Queue<(int, int)> queue = new Queue<(int, int)>(); queue.Enqueue((r, c)); visited[r][c] = true; while (queue.Count > 0) { var (row, col) = queue.Dequeue(); foreach (var dir in directions) { int nr = row + dir[0]; int nc = col + dir[1]; if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == '1' && !visited[nr][nc]) { queue.Enqueue((nr, nc)); visited[nr][nc] = true; } } } } } } return count; } } class Program { static void Main() { char[][] grid = new char[][] { new char[] {'1','1','0','0','0'}, new char[] {'1','1','0','0','0'}, new char[] {'0','0','1','0','0'}, new char[] {'0','0','0','1','1'} }; Solution sol = new Solution(); Console.WriteLine(sol.NumIslands(grid)); } }
Attempts:
2 left
💡 Hint
Count connected groups of '1's using BFS traversal.
✗ Incorrect
The grid has three separate islands: one in the top-left corner, one in the middle, and one in the bottom-right corner.
❓ Predict Output
intermediateOutput of DFS-based Number of Islands
What is the output of the following C# code that counts islands using DFS?
DSA C#
using System; class Solution { private int rows, cols; private char[][] grid; public int NumIslands(char[][] grid) { if (grid == null || grid.Length == 0) return 0; this.grid = grid; rows = grid.Length; cols = grid[0].Length; int count = 0; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { if (grid[r][c] == '1') { count++; DFS(r, c); } } } return count; } private void DFS(int r, int c) { if (r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] == '0') return; grid[r][c] = '0'; DFS(r + 1, c); DFS(r - 1, c); DFS(r, c + 1); DFS(r, c - 1); } } class Program { static void Main() { char[][] grid = new char[][] { new char[] {'1','1','0','0','0'}, new char[] {'1','1','0','0','0'}, new char[] {'0','0','1','0','0'}, new char[] {'0','0','0','1','1'} }; Solution sol = new Solution(); Console.WriteLine(sol.NumIslands(grid)); } }
Attempts:
2 left
💡 Hint
DFS marks connected land as water to avoid recounting.
✗ Incorrect
The DFS visits and marks all connected '1's in each island, counting 3 islands total.
🧠 Conceptual
advancedWhy BFS or DFS is suitable for Number of Islands?
Why are BFS and DFS algorithms suitable for solving the Number of Islands problem?
Attempts:
2 left
💡 Hint
Think about how connected components are found in a grid.
✗ Incorrect
BFS and DFS explore all connected land cells (neighbors) starting from one land cell, which helps identify each island as a connected component.
🔧 Debug
advancedIdentify the bug in this DFS code snippet
What error will this DFS code cause when counting islands?
DSA C#
private void DFS(int r, int c) { if (r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] == '0') return; DFS(r + 1, c); DFS(r - 1, c); DFS(r, c + 1); DFS(r, c - 1); }
Attempts:
2 left
💡 Hint
Check if visited cells are marked to avoid infinite recursion.
✗ Incorrect
The code does not mark visited land cells as water or visited, so it revisits the same cells infinitely causing stack overflow.
🚀 Application
expertOptimizing Number of Islands for large grids
Which approach best optimizes counting islands in a very large grid with mostly water?
Attempts:
2 left
💡 Hint
Think about data structures that efficiently merge connected components.
✗ Incorrect
Union-Find (Disjoint Set) efficiently merges connected land cells and counts islands with near constant time operations, suitable for large grids.
