0
0
DSA Cprogramming~10 mins

Sudoku Solver Using Backtracking 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 check if a number is already used in the given row.

DSA C
int usedInRow(int grid[9][9], int row, int num) {
    for (int col = 0; col < 9; col++) {
        if (grid[row][col] == [1]) {
            return 1;
        }
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Anum
Brow
Ccol
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with 'row' or 'col' instead of 'num'.
Using assignment '=' instead of comparison '=='.
2fill in blank
medium

Complete the code to check if a number is already used in the given column.

DSA C
int usedInCol(int grid[9][9], int col, int num) {
    for (int row = 0; row < 9; row++) {
        if (grid[row][col] == [1]) {
            return 1;
        }
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Anum
Bcol
Crow
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with 'row' or 'col' instead of 'num'.
Using assignment '=' instead of comparison '=='.
3fill in blank
hard

Fix the error in the code that checks if a number is used in the 3x3 box.

DSA C
int usedInBox(int grid[9][9], int boxStartRow, int boxStartCol, int num) {
    for (int row = 0; row < 3; row++) {
        for (int col = 0; col < 3; col++) {
            if (grid[row + boxStartRow][col + boxStartCol] == [1]) {
                return 1;
            }
        }
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
AboxStartCol
Bnum
CboxStartRow
Drow
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with boxStartRow or boxStartCol instead of 'num'.
Using assignment '=' instead of comparison '=='.
4fill in blank
hard

Fill both blanks to complete the function that checks if it is safe to place a number in a cell.

DSA C
int isSafe(int grid[9][9], int row, int col, int num) {
    return !usedInRow(grid, row, num) && !usedInCol(grid, col, num) && !usedInBox(grid, row - row % 3, col - col % 3, [1]) && grid[row][col] == [2];
}
Drag options to blanks, or click blank then click option'
Anum
B0
Crow
Dcol
Attempts:
3 left
💡 Hint
Common Mistakes
Using row or col instead of num in usedInBox.
Checking if grid[row][col] equals num instead of 0.
5fill in blank
hard

Fill all three blanks to complete the backtracking function that solves the Sudoku.

DSA C
int solveSudoku(int grid[9][9]) {
    int row, col;
    int found = 0;
    for (row = 0; row < 9; row++) {
        for (col = 0; col < 9; col++) {
            if (grid[row][col] == 0) {
                found = 1;
                break;
            }
        }
        if (found) break;
    }
    if (!found) return 1;

    for (int num = 1; num <= 9; num++) {
        if (isSafe(grid, row, col, [1])) {
            grid[row][col] = [2];
            if (solveSudoku(grid))
                return [3];
            grid[row][col] = 0;
        }
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Anum
Brow
C1
Dcol
Attempts:
3 left
💡 Hint
Common Mistakes
Using row or col instead of num for placement.
Returning 0 instead of 1 on success.