Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start BFS from the given cell.
DSA C
void bfs(char** grid, int r, int c, int row, int col, int** visited) {
int directions[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int queue[10000][2];
int front = 0, rear = 0;
queue[rear][0] = row;
queue[rear][1] = col;
rear++;
visited[row][col] = 1;
while (front < rear) {
int currRow = queue[front][0];
int currCol = queue[front][1];
front++;
for (int i = 0; i < 4; i++) {
int newRow = currRow + directions[i][0];
int newCol = currCol + directions[i][1];
if (newRow >= 0 && newRow < r && newCol >= 0 && newCol < c &&
grid[newRow][newCol] == '1' && visited[newRow][newCol] == 0) {
queue[rear][0] = newRow;
queue[rear][1] = newCol;
rear++;
visited[newRow][newCol] = [1];
}
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Marking visited cells as 0 which means unvisited.
Not marking visited cells at all causing infinite loops.
✗ Incorrect
Marking the new cell as visited with 1 prevents revisiting it during BFS.
2fill in blank
mediumComplete the code to check if the current cell is land and unvisited before DFS.
DSA C
void dfs(char** grid, int r, int c, int row, int col, int** visited) {
if (row < 0 || row >= r || col < 0 || col >= c ||
grid[row][col] == '0' || visited[row][col] == [1]) {
return;
}
visited[row][col] = 1;
dfs(grid, r, c, row - 1, col, visited);
dfs(grid, r, c, row + 1, col, visited);
dfs(grid, r, c, row, col - 1, visited);
dfs(grid, r, c, row, col + 1, visited);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for visited cells incorrectly as 0 instead of 1.
Not checking boundaries causing out-of-range errors.
✗ Incorrect
We skip cells that are already visited (marked as 1), so we check for 0 (unvisited).
3fill in blank
hardFix the error in the main function to count islands using DFS.
DSA C
int numIslands(char** grid, int r, int c) {
int count = 0;
int** visited = malloc(r * sizeof(int*));
for (int i = 0; i < r; i++) {
visited[i] = calloc(c, sizeof(int));
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (grid[i][j] == '1' && visited[i][j] == 0) {
dfs(grid, r, c, i, j, visited);
count[1];
}
}
}
for (int i = 0; i < r; i++) {
free(visited[i]);
}
free(visited);
return count;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -- which decreases count incorrectly.
Using = which resets count.
✗ Incorrect
We increment count by 1 each time we find a new island.
4fill in blank
hardFill both blanks to complete BFS traversal and mark visited cells.
DSA C
void bfs(char** grid, int r, int c, int row, int col, int** visited) {
int directions[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int queue[10000][2];
int front = 0, rear = 0;
queue[rear][0] = row;
queue[rear][1] = col;
rear++;
visited[row][col] = 1;
while (front < rear) {
int currRow = queue[front][0];
int currCol = queue[front][1];
front++;
for (int i = 0; i < 4; i++) {
int newRow = currRow + directions[[1]][0];
int newCol = currCol + directions[[2]][1];
if (newRow >= 0 && newRow < r && newCol >= 0 && newCol < c &&
grid[newRow][newCol] == '1' && visited[newRow][newCol] == 0) {
queue[rear][0] = newRow;
queue[rear][1] = newCol;
rear++;
visited[newRow][newCol] = 1;
}
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different indices for row and column causing wrong neighbors.
Using constants instead of loop variable i.
✗ Incorrect
We use the loop variable i to access directions for row and column offsets.
5fill in blank
hardFill all three blanks to create a DFS call that explores neighbors correctly.
DSA C
void dfs(char** grid, int r, int c, int row, int col, int** visited) {
if (row < 0 || row >= r || col < 0 || col >= c ||
grid[row][col] == '0' || visited[row][col] == 1) {
return;
}
visited[row][col] = 1;
int directions[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
for (int i = 0; i < 4; i++) {
dfs(grid, r, c, row [1] directions[i][[2]], col [3] directions[i][[2]], visited);
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using - instead of + causing wrong neighbor positions.
Using same index for both row and column offsets.
✗ Incorrect
We add directions[i][0] to row and directions[i][1] to col to explore neighbors.