Bird
Raised Fist0
DSA C++programming~10 mins

Sudoku Solver Using Backtracking in DSA C++ - Interactive Practice

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

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

DSA C++
bool usedInRow(int grid[9][9], int row, int num) {
    for (int col = 0; col < 9; col++) {
        if (grid[row][col] == [1]) {
            return true;
        }
    }
    return false;
}
Drag options to blanks, or click blank then click option'
A0
Brow
Ccol
Dnum
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable to compare, like 'row' or 'col' instead of 'num'.
Checking only the first column instead of all columns.
2fill in blank
medium

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

DSA C++
bool usedInCol(int grid[9][9], int col, int num) {
    for (int row = 0; row < 9; row++) {
        if (grid[row][col] == [1]) {
            return true;
        }
    }
    return false;
}
Drag options to blanks, or click blank then click option'
Acol
Bnum
C0
Drow
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable to compare, like 'row' or 'col' instead of 'num'.
Checking only the first row instead of all rows.
3fill in blank
hard

Fix the error in checking if a number is already in the 3x3 box.

DSA C++
bool 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[boxStartRow + row][boxStartCol + col] == [1]) {
                return true;
            }
        }
    }
    return false;
}
Drag options to blanks, or click blank then click option'
AboxStartRow
BboxStartCol
Cnum
Drow
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with the wrong variable like 'row' or 'boxStartRow'.
Not adding the loop counters to the box start indices.
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++
bool 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 the wrong variable for the number in the box check.
Checking if the cell equals 'row' or 'col' instead of 0.
5fill in blank
hard

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

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

    for (int num = 1; num <= 9; num++) {
        if (isSafe(grid, row, col, [1])) {
            grid[row][col] = [2];
            if (solveSudoku(grid))
                return true;
            grid[row][col] = [3];
        }
    }
    return false;
}
Drag options to blanks, or click blank then click option'
Anum
C0
Drow
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variables for blanks, like 'row' instead of 'num'.
Not resetting the cell to 0 after backtracking.

Practice

(1/5)
1. What is the main idea behind the backtracking technique used in a Sudoku solver?
easy
A. Use a fixed pattern to fill the board
B. Fill the board randomly without checking rules
C. Try placing numbers and undo if they lead to conflicts
D. Only fill cells with a single possible number

Solution

  1. Step 1: Understand backtracking in Sudoku

    Backtracking tries numbers in empty cells and checks if they fit the Sudoku rules.
  2. Step 2: Undo if conflict arises

    If a number causes a conflict later, it is removed (backtracked) and another number is tried.
  3. Final Answer:

    Try placing numbers and undo if they lead to conflicts -> Option C
  4. Quick Check:

    Backtracking = Try and undo [OK]
Hint: Backtracking means try, check, and undo if needed [OK]
Common Mistakes:
  • Thinking backtracking fills randomly
  • Assuming backtracking fills only once without undo
  • Believing backtracking uses fixed patterns
2. Which of the following is the correct way to check if a number can be placed in a Sudoku cell in C++?
easy
A. if (isSafe(board, row, col, num)) { board[row][col] = num; }
B. if (board[row][col] == num) { placeNumber(); }
C. if (num > 9) { board[row][col] = num; }
D. if (board[row][col] != 0) { board[row][col] = num; }

Solution

  1. Step 1: Understand the safety check function

    The function isSafe checks if placing num at board[row][col] follows Sudoku rules.
  2. Step 2: Place number only if safe

    Only if isSafe returns true, the number is placed in the cell.
  3. Final Answer:

    if (isSafe(board, row, col, num)) { board[row][col] = num; } -> Option A
  4. Quick Check:

    Use isSafe before placing number [OK]
Hint: Always check safety before placing number [OK]
Common Mistakes:
  • Placing number without checking rules
  • Checking if cell already has the number incorrectly
  • Using invalid conditions for placement
3. What will be the output of the following code snippet when solving a Sudoku board?
int board[9][9] = {
  {5,3,0,0,7,0,0,0,0},
  {6,0,0,1,9,5,0,0,0},
  {0,9,8,0,0,0,0,6,0},
  {8,0,0,0,6,0,0,0,3},
  {4,0,0,8,0,3,0,0,1},
  {7,0,0,0,2,0,0,0,6},
  {0,6,0,0,0,0,2,8,0},
  {0,0,0,4,1,9,0,0,5},
  {0,0,0,0,8,0,0,7,9}
};
solveSudoku(board);
Assuming solveSudoku correctly solves the puzzle, what will be the value at board[0][2] after solving?
medium
A. 9
B. 4
C. 7
D. 1

Solution

  1. Step 1: Identify the empty cell at (0,2)

    The cell at row 0, column 2 is initially 0 (empty).
  2. Step 2: Solve Sudoku and find correct number

    After solving, the number 4 fits at (0,2) without conflicts in row, column, or box.
  3. Final Answer:

    4 -> Option B
  4. Quick Check:

    board[0][2] = 4 after solve [OK]
Hint: Check solved board cell for correct number [OK]
Common Mistakes:
  • Guessing without checking Sudoku rules
  • Confusing row and column indices
  • Assuming initial zeros remain zeros
4. Consider this buggy code snippet for checking if a number can be placed in a Sudoku cell:
bool isSafe(int board[9][9], int row, int col, int num) {
  for (int x = 0; x < 9; x++) {
    if (board[row][x] == num || board[x][col] == num)
      return false;
  }
  int startRow = row - row % 3;
  int startCol = col - col % 3;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      if (board[i][j] == num)
        return false;
    }
  }
  return true;
}
What is the bug in this code?
medium
A. The 3x3 box check uses wrong indices; should use startRow + i and startCol + j
B. The row and column checks are missing
C. The function does not return false when number is found
D. The modulo operation is incorrect

Solution

  1. Step 1: Analyze 3x3 box checking loop

    The inner loop checks board[i][j] instead of board[startRow + i][startCol + j], so it checks wrong cells.
  2. Step 2: Correct indices for box check

    To check the correct 3x3 box, add startRow and startCol offsets to i and j.
  3. Final Answer:

    The 3x3 box check uses wrong indices; should use startRow + i and startCol + j -> Option A
  4. Quick Check:

    Box check indices must include startRow/startCol [OK]
Hint: Add startRow/startCol offsets when checking 3x3 box [OK]
Common Mistakes:
  • Checking wrong cells in 3x3 box
  • Ignoring row or column checks
  • Incorrect modulo usage
5. You want to optimize a Sudoku solver using backtracking by adding a heuristic. Which of the following strategies best reduces the search space?
hard
A. Fill cells row-wise ignoring constraints until stuck
B. Try numbers in random order for each empty cell
C. Always fill the first empty cell found without checking possibilities
D. Choose the empty cell with the fewest possible valid numbers first

Solution

  1. Step 1: Understand heuristic to reduce search space

    Choosing the cell with the fewest valid options first reduces branching and speeds up solving.
  2. Step 2: Compare other strategies

    Filling first empty cell or random tries increase backtracking steps; ignoring constraints causes errors.
  3. Final Answer:

    Choose the empty cell with the fewest possible valid numbers first -> Option D
  4. Quick Check:

    Minimum remaining values heuristic = choose cell with fewest options [OK]
Hint: Pick cell with least options first to speed solving [OK]
Common Mistakes:
  • Filling cells without checking possibilities
  • Trying numbers randomly wastes time
  • Ignoring constraints leads to wrong solutions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard