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 Rust code that counts islands using BFS?
DSA Rust
fn num_islands_bfs(grid: Vec<Vec<char>>) -> i32 {
use std::collections::VecDeque;
let rows = grid.len();
if rows == 0 { return 0; }
let cols = grid[0].len();
let mut grid = grid;
let mut count = 0;
let directions = [(1,0), (-1,0), (0,1), (0,-1)];
for r in 0..rows {
for c in 0..cols {
if grid[r][c] == '1' {
count += 1;
let mut queue = VecDeque::new();
queue.push_back((r, c));
grid[r][c] = '0';
while let Some((x, y)) = queue.pop_front() {
for (dx, dy) in directions.iter() {
let nx = x as isize + dx;
let ny = y as isize + dy;
if nx >= 0 && nx < rows as isize && ny >= 0 && ny < cols as isize {
let (nx, ny) = (nx as usize, ny as usize);
if grid[nx][ny] == '1' {
queue.push_back((nx, ny));
grid[nx][ny] = '0';
}
}
}
}
}
}
}
count
}
fn main() {
let grid = vec![
vec!['1','1','0','0','0'],
vec!['1','1','0','0','0'],
vec!['0','0','1','0','0'],
vec!['0','0','0','1','1']
];
println!("{}", num_islands_bfs(grid));
}Attempts:
2 left
💡 Hint
Count how many separate groups of '1's are connected horizontally or vertically.
✗ Incorrect
The grid has three separate islands: one at top-left, one in the middle, and one at bottom-right.
❓ Predict Output
intermediateOutput of DFS-based Number of Islands
What is the output of this Rust code that counts islands using DFS?
DSA Rust
fn num_islands_dfs(grid: Vec<Vec<char>>) -> i32 {
fn dfs(grid: &mut Vec<Vec<char>>, r: usize, c: usize) {
if r >= grid.len() || c >= grid[0].len() || grid[r][c] == '0' {
return;
}
grid[r][c] = '0';
if r > 0 { dfs(grid, r - 1, c); }
if c > 0 { dfs(grid, r, c - 1); }
if r + 1 < grid.len() { dfs(grid, r + 1, c); }
if c + 1 < grid[0].len() { dfs(grid, r, c + 1); }
}
let mut grid = grid;
let mut count = 0;
for r in 0..grid.len() {
for c in 0..grid[0].len() {
if grid[r][c] == '1' {
count += 1;
dfs(&mut grid, r, c);
}
}
}
count
}
fn main() {
let grid = vec![
vec!['1','1','0','0','0'],
vec!['1','1','0','0','0'],
vec!['0','0','1','0','0'],
vec!['0','0','0','1','1']
];
println!("{}", num_islands_dfs(grid));
}Attempts:
2 left
💡 Hint
DFS explores all connected land cells recursively.
✗ Incorrect
The DFS marks all connected '1's as '0' and counts 3 islands in the grid.
🧠 Conceptual
advancedWhy BFS or DFS is suitable 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 land cells, marking them visited to count each island once.
🔧 Debug
advancedIdentify the bug in this DFS code snippet
What error will this Rust DFS code cause when counting islands?
DSA Rust
fn dfs(grid: &mut Vec<Vec<char>>, r: usize, c: usize) {
if r >= grid.len() || c >= grid[0].len() || grid[r][c] == '0' {
return;
}
grid[r][c] = '0';
dfs(grid, r - 1, c);
dfs(grid, r, c - 1);
dfs(grid, r + 1, c);
dfs(grid, r, c + 1);
}Attempts:
2 left
💡 Hint
Check how negative indices are handled in Rust usize type.
✗ Incorrect
Subtracting 1 from 0 in usize causes underflow, leading to out-of-bounds access at runtime.
🚀 Application
expertOptimizing Number of Islands for Large Grids
Which approach best optimizes counting islands in a very large grid with mostly water cells?
Attempts:
2 left
💡 Hint
Think about data structures that efficiently merge connected components.
✗ Incorrect
Union-Find efficiently merges connected land cells and counts distinct sets, optimizing large grids.
