Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start BFS from the given cell.
DSA Java
Queue<int[]> queue = new LinkedList<>();
queue.[1](new int[]{row, col});Attempts:
3 left
💡 Hint
Common Mistakes
Using 'push' which is for stacks, not queues.
Using 'insert' which is not a Queue method.
✗ Incorrect
In Java's Queue interface, 'offer' is the preferred method to add elements safely.
2fill in blank
mediumComplete the code to mark the current cell as visited in DFS.
DSA Java
grid[row][col] = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Marking with '1' again which doesn't change the state.
Using '#' or 'X' which are not standard in this problem.
✗ Incorrect
Marking the cell as '0' means water, so it won't be visited again.
3fill in blank
hardFix the error in the BFS neighbor check condition.
DSA Java
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == [1]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for '0' which is water, so BFS won't explore land.
Using '#' or 'X' which are not part of the grid representation.
✗ Incorrect
We only add neighbors that are land cells marked '1' to the queue.
4fill in blank
hardFill both blanks to complete the DFS recursive calls for neighbors.
DSA Java
dfs(grid, row [1] 1, col); dfs(grid, row, col [2] 1);
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction when addition is needed to move forward.
Using multiplication or division which are invalid for index movement.
✗ Incorrect
We move to adjacent cells by adding or subtracting 1 from row or col.
5fill in blank
hardFill all three blanks to complete the BFS neighbor exploration loop.
DSA Java
int[] dr = [1]; int[] dc = [2]; for (int i = 0; i < 4; i++) { int nr = r + dr[i]; int nc = c + dc[i]; if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == [3]) { queue.offer(new int[]{nr, nc}); grid[nr][nc] = '0'; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up dr and dc arrays.
Checking for '0' instead of '1' which skips land cells.
✗ Incorrect
dr and dc arrays represent row and column changes for neighbors; we check for land '1' cells.
