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 Go code that counts islands using BFS?
DSA Go
package main import "fmt" func numIslands(grid [][]byte) int { if len(grid) == 0 { return 0 } rows, cols := len(grid), len(grid[0]) count := 0 directions := [][]int{{1,0},{-1,0},{0,1},{0,-1}} var bfs func(r, c int) bfs = func(r, c int) { queue := [][]int{{r, c}} grid[r][c] = '0' for len(queue) > 0 { curr := queue[0] queue = queue[1:] for _, d := range directions { nr, nc := curr[0]+d[0], curr[1]+d[1] if nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == '1' { queue = append(queue, []int{nr, nc}) grid[nr][nc] = '0' } } } } for r := 0; r < rows; r++ { for c := 0; c < cols; c++ { if grid[r][c] == '1' { bfs(r, c) count++ } } } return count } func main() { grid := [][]byte{ {'1','1','0','0','0'}, {'1','1','0','0','0'}, {'0','0','1','0','0'}, {'0','0','0','1','1'}, } fmt.Println(numIslands(grid)) }
Attempts:
2 left
💡 Hint
Count how many separate groups of '1's are connected horizontally or vertically.
✗ Incorrect
The BFS visits all connected land cells ('1') and marks them as water ('0'). The grid has three separate islands: top-left, middle, and bottom-right.
❓ Predict Output
intermediateOutput of DFS-based Number of Islands
What is the output of the following Go code that counts islands using DFS?
DSA Go
package main import "fmt" func numIslandsDFS(grid [][]byte) int { if len(grid) == 0 { return 0 } rows, cols := len(grid), len(grid[0]) count := 0 var dfs func(r, c int) dfs = func(r, c int) { 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) } for r := 0; r < rows; r++ { for c := 0; c < cols; c++ { if grid[r][c] == '1' { dfs(r, c) count++ } } } return count } func main() { grid := [][]byte{ {'1','1','1','1','0'}, {'1','1','0','1','0'}, {'1','1','0','0','0'}, {'0','0','0','0','0'}, } fmt.Println(numIslandsDFS(grid)) }
Attempts:
2 left
💡 Hint
DFS marks all connected land cells as water recursively.
✗ Incorrect
The grid has one big island connected horizontally and vertically.
🔧 Debug
advancedIdentify the error in BFS island counting code
What error will this Go code produce when counting islands using BFS?
DSA Go
package main import "fmt" func numIslands(grid [][]byte) int { rows, cols := len(grid), len(grid[0]) count := 0 directions := [][]int{{1,0},{-1,0},{0,1},{0,-1}} var bfs func(r, c int) bfs = func(r, c int) { queue := [][]int{{r, c}} grid[r][c] = '0' for len(queue) > 0 { curr := queue[0] queue = queue[1:] for _, d := range directions { nr, nc := curr[0]+d[0], curr[1]+d[1] if nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == '1' { queue = append(queue, []int{nr, nc}) grid[nr][nc] = '0' } } } } for r := 0; r < rows; r++ { for c := 0; c < cols; c++ { if grid[r][c] == '1' { bfs(r, c) count++ } } } return count } func main() { var grid [][]byte fmt.Println(numIslands(grid)) }
Attempts:
2 left
💡 Hint
Check how the code handles empty grid input.
✗ Incorrect
The code does not check if grid is empty before accessing grid[0], causing index out of range error.
🧠 Conceptual
advancedWhy use BFS or DFS for Number of Islands?
Why do BFS and DFS work well for counting islands in a grid?
Attempts:
2 left
💡 Hint
Think about how connected components are found in graphs.
✗ Incorrect
BFS and DFS traverse all connected cells of an island, marking them visited to count each island once.
🚀 Application
expertNumber of Islands with Diagonal Connections
Given the following grid, how many islands are counted if diagonal neighbors are also considered connected?
DSA Go
grid := [][]byte{
{'1','0','1','0'},
{'0','1','0','1'},
{'1','0','1','0'},
{'0','1','0','1'},
}Attempts:
2 left
💡 Hint
Consider diagonal neighbors as connected when counting islands.
✗ Incorrect
With diagonal connections (8-directional connectivity), all eight '1's form a single connected component in the checkerboard pattern.
