Challenge - 5 Problems
Island Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of BFS traversal on grid
What is the number of islands found by BFS in the given grid?
DSA Typescript
const grid = [ ['1','1','0','0','0'], ['1','1','0','0','0'], ['0','0','1','0','0'], ['0','0','0','1','1'] ]; function numIslandsBFS(grid: string[][]): number { if (!grid.length) return 0; const rows = grid.length; const cols = grid[0].length; let count = 0; const directions = [[1,0],[-1,0],[0,1],[0,-1]]; for (let r = 0; r < rows; r++) { for (let c = 0; c < cols; c++) { if (grid[r][c] === '1') { count++; const queue = [[r,c]]; grid[r][c] = '0'; while(queue.length) { const [x,y] = queue.shift()!; for (const [dx,dy] of directions) { const nx = x + dx; const ny = y + dy; if (nx >= 0 && nx < rows && ny >= 0 && ny < cols && grid[nx][ny] === '1') { queue.push([nx, ny]); grid[nx][ny] = '0'; } } } } } } return count; } console.log(numIslandsBFS(grid));
Attempts:
2 left
💡 Hint
Count connected groups of '1's using BFS.
✗ Incorrect
The BFS visits each island once and marks visited cells. There are three islands in the grid.
❓ Predict Output
intermediate2:00remaining
Output of DFS traversal on grid
What is the number of islands found by DFS in the given grid?
DSA Typescript
const grid = [ ['1','1','0','0','0'], ['1','1','0','0','0'], ['0','0','1','0','0'], ['0','0','0','1','1'] ]; function numIslandsDFS(grid: string[][]): number { if (!grid.length) return 0; const rows = grid.length; const cols = grid[0].length; let count = 0; function dfs(r: number, c: number) { 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 (let r = 0; r < rows; r++) { for (let c = 0; c < cols; c++) { if (grid[r][c] === '1') { count++; dfs(r, c); } } } return count; } console.log(numIslandsDFS(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 islands.
🧠 Conceptual
advanced1:30remaining
Why mark visited cells in Number of Islands?
Why do BFS and DFS algorithms mark visited land cells as '0' or visited when counting islands?
Attempts:
2 left
💡 Hint
Think about what happens if you revisit the same land cell.
✗ Incorrect
Marking visited cells prevents revisiting and recounting the same island multiple times.
❓ Predict Output
advanced2:00remaining
Output after modifying grid during DFS
What is the final state of the grid after running DFS-based number of islands on this input?
DSA Typescript
const grid = [ ['1','0','1'], ['0','1','0'], ['1','0','1'] ]; function numIslandsDFS(grid: string[][]): number { if (!grid.length) return 0; const rows = grid.length; const cols = grid[0].length; let count = 0; function dfs(r: number, c: number) { 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 (let r = 0; r < rows; r++) { for (let c = 0; c < cols; c++) { if (grid[r][c] === '1') { count++; dfs(r, c); } } } return count; } numIslandsDFS(grid); console.log(grid);
Attempts:
2 left
💡 Hint
DFS changes all connected '1's to '0's.
✗ Incorrect
DFS visits each island cell and marks it '0'. All islands are single cells here, so all become '0'.
🧠 Conceptual
expert1:30remaining
Time complexity of Number of Islands algorithms
What is the time complexity of BFS or DFS approach to count the number of islands in an m x n grid?
Attempts:
2 left
💡 Hint
Consider how many times each cell is processed.
✗ Incorrect
Each cell is visited once during BFS or DFS, so complexity is proportional to total cells.