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 Java code that counts islands using BFS?
DSA Java
public class Solution { public int numIslands(char[][] grid) { if (grid == null || grid.length == 0) return 0; int count = 0; int rows = grid.length, cols = grid[0].length; boolean[][] visited = new boolean[rows][cols]; 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]) { count++; java.util.Queue<int[]> queue = new java.util.LinkedList<>(); queue.offer(new int[]{r,c}); visited[r][c] = true; while (!queue.isEmpty()) { int[] cell = queue.poll(); for (int[] d : directions) { int nr = cell[0] + d[0], nc = cell[1] + d[1]; if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == '1' && !visited[nr][nc]) { queue.offer(new int[]{nr,nc}); visited[nr][nc] = true; } } } } } } return count; } public static void main(String[] args) { char[][] grid = { {'1','1','0','0','0'}, {'1','1','0','0','0'}, {'0','0','1','0','0'}, {'0','0','0','1','1'} }; Solution sol = new Solution(); System.out.println(sol.numIslands(grid)); } }
Attempts:
2 left
💡 Hint
Count connected groups of '1's using BFS.
✗ Incorrect
The grid has three separate islands: top-left 2x2 block, middle single cell, and bottom-right 2 cells connected.
❓ Predict Output
intermediateOutput of DFS-based Number of Islands
What is the output of the following Java code that counts islands using DFS?
DSA Java
public class Solution { private void dfs(char[][] grid, int r, int c, boolean[][] visited) { int rows = grid.length, cols = grid[0].length; if (r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] == '0' || visited[r][c]) return; visited[r][c] = true; dfs(grid, r+1, c, visited); dfs(grid, r-1, c, visited); dfs(grid, r, c+1, visited); dfs(grid, r, c-1, visited); } public int numIslands(char[][] grid) { if (grid == null || grid.length == 0) return 0; int count = 0; int rows = grid.length, cols = grid[0].length; boolean[][] visited = new boolean[rows][cols]; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { if (grid[r][c] == '1' && !visited[r][c]) { count++; dfs(grid, r, c, visited); } } } return count; } public static void main(String[] args) { char[][] grid = { {'1','1','0','0','0'}, {'1','1','0','0','0'}, {'0','0','1','0','0'}, {'0','0','0','1','1'} }; Solution sol = new Solution(); System.out.println(sol.numIslands(grid)); } }
Attempts:
2 left
💡 Hint
Use DFS to mark all connected land cells.
✗ Incorrect
DFS explores each island fully, counting 3 islands in the grid.
🧠 Conceptual
advancedWhy BFS or DFS is suitable for Number of Islands?
Why do BFS and DFS work well to solve the Number of Islands problem?
Attempts:
2 left
💡 Hint
Think about how connected components are found in graphs.
✗ Incorrect
BFS and DFS traverse connected nodes (land cells) fully, marking them visited to count distinct islands.
🔧 Debug
advancedIdentify the bug in this DFS code for Number of Islands
What error will this DFS code cause when counting islands?
DSA Java
private void dfs(char[][] grid, int r, int c, boolean[][] visited) { int rows = grid.length, cols = grid[0].length; if (r < 0 || c < 0 || r >= rows || c >= cols || grid[r][c] == '0') return; visited[r][c] = true; dfs(grid, r+1, c, visited); dfs(grid, r-1, c, visited); dfs(grid, r, c+1, visited); dfs(grid, r, c-1, visited); } public int numIslands(char[][] grid) { int count = 0; int rows = grid.length, cols = grid[0].length; boolean[][] visited = new boolean[rows][cols]; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { if (grid[r][c] == '1' && !visited[r][c]) { count++; dfs(grid, r, c, visited); } } } return count; }
Attempts:
2 left
💡 Hint
Check the DFS base case conditions carefully.
✗ Incorrect
The DFS does not check if a cell is already visited before recursing, causing infinite recursion.
🚀 Application
expertOptimizing Number of Islands with Union-Find
Which statement about using Union-Find (Disjoint Set) to solve Number of Islands is true?
Attempts:
2 left
💡 Hint
Think about how Union-Find groups connected components.
✗ Incorrect
Union-Find merges connected land cells into sets, allowing quick counting of distinct islands without recursion.
