0
0
DSA Cprogramming~10 mins

Number of Islands BFS and DFS in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A2
B1
C-1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Marking visited cells as 0 which means unvisited.
Not marking visited cells at all causing infinite loops.
2fill in blank
medium

Complete 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'
A0
B2
C1
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for visited cells incorrectly as 0 instead of 1.
Not checking boundaries causing out-of-range errors.
3fill in blank
hard

Fix 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'
A--
B+=
C++
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using -- which decreases count incorrectly.
Using = which resets count.
4fill in blank
hard

Fill 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'
Ai
B0
C1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using different indices for row and column causing wrong neighbors.
Using constants instead of loop variable i.
5fill in blank
hard

Fill 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'
A+
B-
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using - instead of + causing wrong neighbor positions.
Using same index for both row and column offsets.