Bird
Raised Fist0
DSA Cprogramming~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 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.

Practice

(1/5)
1. What is the main purpose of backtracking in a Sudoku solver?
easy
A. To sort the Sudoku grid before solving
B. To try possible numbers and undo choices if they lead to conflicts
C. To fill all empty cells with zeros
D. To randomly assign numbers to cells

Solution

  1. Step 1: Understand backtracking role

    Backtracking tries numbers in empty cells and removes them if they cause conflicts.
  2. Step 2: Compare other options

    Sorting or random assignment does not solve Sudoku logically; filling zeros is incorrect.
  3. Final Answer:

    To try possible numbers and undo choices if they lead to conflicts -> Option B
  4. Quick Check:

    Backtracking = Try and undo choices [OK]
Hint: Backtracking means try and undo wrong moves [OK]
Common Mistakes:
  • Thinking backtracking sorts the grid
  • Assuming backtracking fills zeros
  • Believing numbers are assigned randomly
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(row, col, num)) { place num; }
B. if isSafe(row, col, num) { place num; }
C. if isSafe(row, col, num); { place num; }
D. if (isSafe[row, col, num]) { place num; }

Solution

  1. Step 1: Understand C syntax for if condition

    In C, conditions must be inside parentheses and followed by braces for blocks.
  2. Step 2: Analyze options

    if (isSafe(row, col, num)) { place num; } uses correct syntax: if(condition) { ... }. Others miss parentheses or use wrong brackets.
  3. Final Answer:

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

    C if syntax = if(condition) { } [OK]
Hint: Use parentheses around conditions in C if statements [OK]
Common Mistakes:
  • Omitting parentheses in if condition
  • Using square brackets instead of parentheses
  • Adding semicolon after if condition
3. What will be the output of the following code snippet that tries to solve a Sudoku puzzle using backtracking?
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}
};
if (solveSudoku(board)) {
  printBoard(board);
} else {
  printf("No solution exists\n");
}
medium
A. Infinite loop occurs
B. No solution exists
C. Compilation error due to missing function
D. The completed Sudoku board printed correctly

Solution

  1. Step 1: Understand the given board and solveSudoku function

    The board is a valid Sudoku puzzle with empty cells as 0. solveSudoku uses backtracking to fill it.
  2. Step 2: Predict output of solveSudoku

    Since the puzzle is solvable, solveSudoku returns true and printBoard prints the completed board.
  3. Final Answer:

    The completed Sudoku board printed correctly -> Option D
  4. Quick Check:

    Solvable board = completed print [OK]
Hint: Valid puzzles print solved board, else print no solution [OK]
Common Mistakes:
  • Assuming no solution exists for valid puzzles
  • Expecting compilation error without full code
  • Thinking infinite loop happens without base case
4. Identify the bug in this backtracking Sudoku solver snippet:
bool solveSudoku(int board[9][9]) {
  int row, col;
  if (!findEmptyCell(board, &row, &col))
    return true;
  for (int num = 1; num <= 9; num++) {
    if (isSafe(board, row, col, num)) {
      board[row][col] = num;
      if (solveSudoku(board))
        return true;
      board[row][col] = 0;
    }
  }
  return false;
}
medium
A. Incorrect loop range for numbers (should be 0 to 9)
B. Not resetting cell to 0 after backtracking
C. No bug; code works correctly
D. Missing base case to stop recursion

Solution

  1. Step 1: Check base case and recursion

    Base case returns true if no empty cell found, stopping recursion correctly.
  2. Step 2: Verify backtracking steps

    After trying a number, if it fails, cell resets to 0, allowing other tries.
  3. Step 3: Confirm loop range

    Loop runs from 1 to 9, which is correct for Sudoku numbers.
  4. Final Answer:

    No bug; code works correctly -> Option C
  5. Quick Check:

    Backtracking code correct = no bug [OK]
Hint: Check base case, reset cell, and loop range carefully [OK]
Common Mistakes:
  • Forgetting to reset cell after failed attempt
  • Wrong loop range including 0
  • Missing base case causing infinite recursion
5. Given a partially filled Sudoku board, which strategy improves backtracking efficiency the most?
hard
A. Use constraint propagation to reduce possible numbers before backtracking
B. Always try numbers 1 to 9 in order for each empty cell
C. Fill empty cells randomly and backtrack if conflict occurs
D. Skip checking row and column constraints to save time

Solution

  1. Step 1: Understand backtracking efficiency

    Trying all numbers blindly is slow; skipping checks causes wrong solutions.
  2. Step 2: Analyze constraint propagation

    Constraint propagation narrows choices before backtracking, reducing search space and speeding solving.
  3. Step 3: Evaluate other options

    Random filling is inefficient; skipping constraints breaks Sudoku rules.
  4. Final Answer:

    Use constraint propagation to reduce possible numbers before backtracking -> Option A
  5. Quick Check:

    Constraint propagation = faster solving [OK]
Hint: Reduce choices first, then backtrack for speed [OK]
Common Mistakes:
  • Trying numbers in fixed order without pruning
  • Random filling wastes time
  • Ignoring constraints breaks puzzle rules
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