Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start BFS from the given cell.
DSA Rust
fn bfs(grid: &mut Vec<Vec<char>>, start_row: usize, start_col: usize) {
let mut queue = std::collections::VecDeque::new();
queue.push_back((start_row, start_col));
grid[start_row][start_col] = '0';
while let Some((row, col)) = queue.pop_front() {
for (dr, dc) in [(1isize, 0isize), (-1isize, 0isize), (0isize, 1isize), (0isize, -1isize)] {
let new_row = row as isize + dr;
let new_col = col as isize + [1];
if new_row >= 0 && new_row < grid.len() as isize && new_col >= 0 && new_col < grid[0].len() as isize {
let (nr, nc) = (new_row as usize, new_col as usize);
if grid[nr][nc] == '1' {
grid[nr][nc] = '0';
queue.push_back((nr, nc));
}
}
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping dr and dc in coordinate calculations.
Using row instead of dr or col instead of dc.
✗ Incorrect
The new column index should be calculated by adding dc (change in column) to the current column.
2fill in blank
mediumComplete the code to check if a cell is within grid bounds.
DSA Rust
fn is_valid(grid: &Vec<Vec<char>>, row: isize, col: isize) -> bool {
row >= 0 && row < [1] as isize && col >= 0 && col < grid[0].len() as isize
}Attempts:
3 left
💡 Hint
Common Mistakes
Using grid[0].len() for row bounds instead of grid.len().
Using -1 in bounds check incorrectly.
✗ Incorrect
The number of rows is grid.len(), so row must be less than grid.len() to be valid.
3fill in blank
hardFix the error in DFS recursion to avoid stack overflow by marking visited cells.
DSA Rust
fn dfs(grid: &mut Vec<Vec<char>>, row: usize, col: usize) {
if row >= grid.len() || col >= grid[0].len() || grid[row][col] == '0' {
return;
}
grid[row][col] = [1];
for (dr, dc) in [(1isize, 0isize), (-1isize, 0isize), (0isize, 1isize), (0isize, -1isize)] {
let new_row = row as isize + dr;
let new_col = col as isize + dc;
if new_row >= 0 && new_row < grid.len() as isize && new_col >= 0 && new_col < grid[0].len() as isize {
dfs(grid, new_row as usize, new_col as usize);
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Marking visited cells as '1' again causing infinite recursion.
Using arbitrary characters that don't match grid representation.
✗ Incorrect
Marking the cell as '0' means it is visited and prevents revisiting, avoiding infinite recursion.
4fill in blank
hardFill both blanks to create a dictionary comprehension that counts islands using DFS.
DSA Rust
fn num_islands(grid: &mut Vec<Vec<char>>) -> i32 {
let mut count = 0;
for row in 0..grid.len() {
for col in 0..grid[0].len() {
if grid[row][col] == [1] {
dfs(grid, row, col);
count += [2];
}
}
}
count
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking for '0' instead of '1' to find islands.
Incrementing count by 0 or wrong values.
✗ Incorrect
We check for '1' to find land and increment count by 1 for each island found.
5fill in blank
hardFill all three blanks to complete BFS traversal updating grid and queue.
DSA Rust
fn bfs(grid: &mut Vec<Vec<char>>, start_row: usize, start_col: usize) {
let mut queue = std::collections::VecDeque::new();
queue.push_back((start_row, start_col));
grid[start_row][start_col] = [1];
while let Some((row, col)) = queue.pop_front() {
for (dr, dc) in [(1isize, 0isize), (-1isize, 0isize), (0isize, 1isize), (0isize, -1isize)] {
let new_row = row as isize + dr;
let new_col = col as isize + dc;
if new_row >= 0 && new_row < grid.len() as isize && new_col >= 0 && new_col < grid[0].len() as isize {
let (nr, nc) = (new_row as usize, new_col as usize);
if grid[nr][nc] == [2] {
grid[nr][nc] = [3];
queue.push_back((nr, nc));
}
}
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Marking visited cells as '1' causing infinite loops.
Checking for '0' instead of '1' to find land.
✗ Incorrect
Mark starting and visited land cells as '0' to indicate visited water; check for '1' to find land.
