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 code that counts islands using BFS?
DSA Python
from collections import deque def num_islands_bfs(grid): if not grid: return 0 rows, cols = len(grid), len(grid[0]) visited = [[False]*cols for _ in range(rows)] def bfs(r, c): queue = deque() queue.append((r, c)) visited[r][c] = True while queue: row, col = queue.popleft() for x, y in [(row-1,col),(row+1,col),(row,col-1),(row,col+1)]: if 0 <= x < rows and 0 <= y < cols and not visited[x][y] and grid[x][y] == '1': visited[x][y] = True queue.append((x, y)) count = 0 for i in range(rows): for j in range(cols): if grid[i][j] == '1' and not visited[i][j]: bfs(i, j) count += 1 return count grid = [ ['1','1','0','0','0'], ['1','1','0','0','0'], ['0','0','1','0','0'], ['0','0','0','1','1'] ] print(num_islands_bfs(grid))
Attempts:
2 left
💡 Hint
Count connected groups of '1's using BFS traversal.
✗ Incorrect
The grid has three separate groups of connected '1's. BFS visits all connected cells in one group before moving to the next.
❓ Predict Output
intermediateOutput of DFS-based Number of Islands
What is the output of the following code that counts islands using DFS?
DSA Python
def num_islands_dfs(grid): if not grid: return 0 rows, cols = len(grid), len(grid[0]) visited = [[False]*cols for _ in range(rows)] def dfs(r, c): if r < 0 or c < 0 or r >= rows or c >= cols or grid[r][c] == '0' or visited[r][c]: return visited[r][c] = True dfs(r-1, c) dfs(r+1, c) dfs(r, c-1) dfs(r, c+1) count = 0 for i in range(rows): for j in range(cols): if grid[i][j] == '1' and not visited[i][j]: dfs(i, j) count += 1 return count grid = [ ['1','1','0','0','0'], ['1','1','0','0','0'], ['0','0','1','0','0'], ['0','0','0','1','1'] ] print(num_islands_dfs(grid))
Attempts:
2 left
💡 Hint
Use DFS to mark all connected land cells.
✗ Incorrect
DFS explores all connected land cells recursively. The grid has three distinct islands.
🧠 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 explored.
✗ Incorrect
BFS and DFS traverse all connected land cells starting from one cell, marking them visited to avoid recounting, thus counting each island exactly once.
🔧 Debug
advancedIdentify the error in this DFS code for Number of Islands
What error will this code produce when run on a grid with islands?
Code:
def dfs(r, c):
if r < 0 or c < 0 or r >= rows or c >= cols or grid[r][c] == '0':
return
visited[r][c] = True
dfs(r-1, c)
dfs(r+1, c)
dfs(r, c-1)
dfs(r, c+1)
count = 0
for i in range(rows):
for j in range(cols):
if grid[i][j] == '1' and not visited[i][j]:
dfs(i, j)
count += 1
Attempts:
2 left
💡 Hint
Check if all variables are declared before use.
✗ Incorrect
The code uses 'visited' without defining or initializing it, causing a NameError.
🚀 Application
expertCount islands with diagonal connections
Modify the standard Number of Islands problem to count islands where diagonally adjacent '1's are also connected. Which change to the BFS or DFS neighbor checks is correct?
Attempts:
2 left
💡 Hint
Think about which neighbors define connectivity including diagonals.
✗ Incorrect
Including diagonal neighbors in BFS or DFS ensures all connected land cells diagonally touching are counted as one island.
