Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start BFS from the given cell.
DSA Go
func bfs(grid [][]byte, visited [][]bool, row int, col int) {
queue := [][]int{{row, col}}
visited[row][col] = true
directions := [][]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
for len(queue) > 0 {
cell := queue[0]
queue = queue[1:]
r, c := cell[0], cell[1]
for _, dir := range directions {
nr, nc := r+dir[0], c+dir[1]
if nr >= 0 && nr < len(grid) && nc >= 0 && nc < len(grid[0]) && grid[nr][nc] == '1' && !visited[nr][nc] {
queue = append(queue, []int{nr, nc})
visited[nr][nc] = [1]
}
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to mark the cell as visited.
Marking visited after popping from the queue instead of when adding.
✗ Incorrect
We mark the neighbor cell as visited by setting visited[nr][nc] = true to avoid revisiting it.
2fill in blank
mediumComplete the DFS function to explore all connected land cells.
DSA Go
func dfs(grid [][]byte, visited [][]bool, row int, col int) {
if row < 0 || row >= len(grid) || col < 0 || col >= len(grid[0]) || grid[row][col] == '0' || visited[row][col] {
return
}
visited[row][col] = true
directions := [][]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
for _, dir := range directions {
dfs(grid, visited, row+[1], col+dir[1])
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the same direction index for both row and column.
Not checking boundaries before recursion.
✗ Incorrect
We add dir[0] to row and dir[1] to col separately. Here, the blank is for row+dir[0].
3fill in blank
hardFix the error in the DFS call to correctly explore neighbors.
DSA Go
func dfs(grid [][]byte, visited [][]bool, row int, col int) {
if row < 0 || row >= len(grid) || col < 0 || col >= len(grid[0]) || grid[row][col] == '0' || visited[row][col] {
return
}
visited[row][col] = true
directions := [][]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
for _, dir := range directions {
dfs(grid, visited, row+dir[0], col+[1])
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using dir[0] for both row and column updates.
Swapping row and column indices.
✗ Incorrect
The column index should be updated by dir[1] to move correctly in the grid.
4fill in blank
hardFill both blanks to count islands using BFS.
DSA Go
func numIslands(grid [][]byte) int {
if len(grid) == 0 {
return 0
}
visited := make([][]bool, len(grid))
for i := range visited {
visited[i] = make([]bool, len(grid[0]))
}
count := 0
for i := 0; i < len(grid); i++ {
for j := 0; j < len(grid[0]); j++ {
if grid[i][j] == '1' && !visited[i][j] {
[1](grid, visited, i, j)
count[2]
}
}
}
return count
}Attempts:
3 left
💡 Hint
Common Mistakes
Calling dfs instead of bfs.
Decrementing count instead of incrementing.
✗ Incorrect
We call bfs to explore the island and increment count by 1 after each BFS.
5fill in blank
hardFill all three blanks to count islands using DFS.
DSA Go
func numIslandsDFS(grid [][]byte) int {
if len(grid) == 0 {
return 0
}
visited := make([][]bool, len(grid))
for i := range visited {
visited[i] = make([]bool, len(grid[0]))
}
count := 0
for i := 0; i < len(grid); i++ {
for j := 0; j < len(grid[0]); j++ {
if grid[i][j] == '1' && !visited[i][j] {
[1](grid, visited, i, j)
count[2]
}
}
}
return count
}Attempts:
3 left
💡 Hint
Common Mistakes
Calling bfs instead of dfs.
Decrementing count instead of incrementing.
✗ Incorrect
We call dfs to explore islands and increment count by 1 after each DFS.
