0
0
DSA Typescriptprogramming~20 mins

Sudoku Solver Using Backtracking in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sudoku Solver Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of the Sudoku board after one step of backtracking?

Given the initial Sudoku board state, what will be the board after the first successful number placement by the backtracking algorithm?

DSA Typescript
const board = [
  [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]
];

// After one step of backtracking, the first 0 replaced with a valid number
// What is the updated board?
A[[5,3,1,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]]
B[[5,3,3,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]]
C[[5,3,2,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]]
D[[5,3,4,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]]
Attempts:
2 left
💡 Hint

Look for the first empty cell (0) and find the smallest valid number that fits according to Sudoku rules.

🧠 Conceptual
intermediate
1:30remaining
Which condition must be checked before placing a number in Sudoku backtracking?

Before placing a number in an empty cell during Sudoku backtracking, which of the following must be true?

AThe number is prime and not present in the same column only.
BThe number is greater than all numbers already placed in the board.
CThe number is even and not present in the same row only.
DThe number is not present in the same row, column, and 3x3 box.
Attempts:
2 left
💡 Hint

Sudoku rules require unique numbers in row, column, and box.

🔧 Debug
advanced
2:30remaining
What error occurs in this Sudoku backtracking code snippet?

Consider this TypeScript snippet for checking if a number can be placed:

function isSafe(board: number[][], row: number, col: number, num: number): boolean {
  for (let x = 0; x < 9; x++) {
    if (board[row][x] === num || board[x][col] === num) {
      return false;
    }
  }
  const startRow = row - row % 3;
  const startCol = col - col % 3;
  for (let i = 0; i < 3; i++) {
    for (let j = 0; j < 3; j++) {
      if (board[startRow + i][startCol + j] === num) {
        return false;
      }
    }
  }
  return true;
}

// What error does this code cause if any?
ARuntime error due to accessing undefined indices in board array.
BNo error; the code runs correctly and returns true or false properly.
CSyntax error because of missing semicolons in the for loops.
DLogical error: the 3x3 box check uses wrong start indices causing wrong validation.
Attempts:
2 left
💡 Hint

Check the calculation of startRow and startCol and the loops for the 3x3 box.

🚀 Application
advanced
2:00remaining
How many recursive calls are made to solve a Sudoku board with backtracking?

Given a Sudoku board with 40 empty cells, approximately how many recursive calls will the backtracking algorithm make in the worst case?

ALess than 40 recursive calls because backtracking prunes invalid paths early.
BExactly 40 recursive calls, one per empty cell.
CUp to 9^40 recursive calls, since each cell can try 9 numbers.
DInfinite recursive calls due to no base case in backtracking.
Attempts:
2 left
💡 Hint

Consider the branching factor and depth of recursion in backtracking.

Predict Output
expert
3:00remaining
What is the final solved Sudoku board after running the solver on this input?

Given this incomplete Sudoku board, what is the fully solved board after the backtracking solver completes?

DSA Typescript
const board = [
  [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]
];

// What is the solved board?
A[[5,3,4,6,7,8,9,1,2],[6,7,2,1,9,5,3,4,8],[1,9,8,3,4,2,5,6,7],[8,5,9,7,6,1,4,2,3],[4,2,6,8,5,3,7,9,1],[7,1,3,9,2,4,8,5,6],[9,6,1,5,3,7,2,8,4],[2,8,7,4,1,9,6,3,5],[3,4,5,2,8,6,1,7,9]]
B[5,3,4,6,7,8,9,1,2],[6,7,2,1,9,5,3,4,8],[1,9,8,3,4,2,5,6,7],[8,5,9,7,6,1,4,2,3],[4,2,6,8,5,3,7,9,1],[7,1,3,9,2,4,8,5,6],[9,6,1,5,3,7,2,8,4],[2,8,7,4,1,9,6,3,5],[3,4,5,2,8,6,1,7,9]]
C[[5,3,4,6,7,8,9,2,1],[6,7,2,1,9,5,3,4,8],[1,9,8,3,4,2,5,6,7],[8,5,9,7,6,1,4,3,2],[4,2,6,8,5,3,7,9,1],[7,1,3,9,2,4,8,5,6],[9,6,1,5,3,7,2,8,4],[2,8,7,4,1,9,6,3,5],[3,4,5,2,8,6,1,7,9]]
D]]9,7,1,6,8,2,5,4,3[,]5,3,6,9,1,4,7,8,2[,]4,8,2,7,3,5,1,6,9[,]6,5,8,4,2,9,3,1,7[,]1,9,7,3,5,8,6,2,4[,]3,2,4,1,6,7,9,5,8[,]7,6,5,2,4,3,8,9,1[,]8,4,3,5,9,1,2,7,6[,]2,1,9,8,7,6,4,3,5[[
Attempts:
2 left
💡 Hint

Check carefully the placement of numbers in the 8th and 9th columns of the first row.