Sudoku Solver Using Backtracking in DSA Javascript - Time & Space Complexity
We want to understand how the time needed to solve a Sudoku puzzle grows as the puzzle size or difficulty changes.
Specifically, how does the backtracking method's work increase with more empty cells?
Analyze the time complexity of the following Sudoku solver code using backtracking.
function solveSudoku(board) {
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 9; col++) {
if (board[row][col] === 0) {
for (let num = 1; num <= 9; num++) {
if (isValid(board, row, col, num)) {
board[row][col] = num;
if (solveSudoku(board)) return true;
board[row][col] = 0;
}
}
return false;
}
}
}
return true;
}
This code tries numbers 1 to 9 in empty cells, checks if valid, and uses recursion to fill the board.
Look at the loops and recursion that repeat work.
- Primary operation: Trying numbers 1 to 9 in each empty cell and recursively solving the rest.
- How many times: Up to 9 tries per empty cell, repeated recursively for each empty cell.
Each empty cell can try up to 9 numbers, and for each try, the solver moves to the next empty cell, repeating this process.
| Empty Cells (n) | Approx. Operations |
|---|---|
| 10 | Up to 9^10 = 3,486,784,401 tries |
| 20 | Up to 9^20 = very large (over 10^19) tries |
| 30 | Up to 9^30 = extremely huge number of tries |
Pattern observation: The number of tries grows exponentially with the number of empty cells.
Time Complexity: O(9^n)
This means the time needed can grow very fast as the number of empty cells increases, because each cell tries many possibilities recursively.
[X] Wrong: "The solver always tries all 9 numbers for every empty cell."
[OK] Correct: The solver stops trying numbers once it finds a valid number that leads to a solution, so it often tries fewer than 9 per cell.
Understanding this exponential growth helps you explain why backtracking can be slow and why pruning or smarter methods matter, a skill valued in problem solving.
"What if we used a heuristic to pick the next empty cell with the fewest valid options? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand backtracking approach
Backtracking tries numbers in empty cells one by one and checks if they fit the Sudoku rules.Step 2: Handle conflicts by backtracking
If a number causes a conflict, the algorithm removes it and tries the next number, going back if needed.Final Answer:
Try placing numbers one by one and backtrack if a conflict occurs -> Option AQuick Check:
Backtracking = Try and undo [OK]
- Thinking backtracking fills cells randomly
- Assuming sorting solves Sudoku
- Replacing zeros without checks
Solution
Step 1: Understand JavaScript if syntax
JavaScript requires parentheses around the condition in an if statement.Step 2: Check function call syntax
Function calls need parentheses with arguments inside, like isSafe(board, row, col, num).Final Answer:
if (isSafe(board, row, col, num)) { placeNumber(); } -> Option DQuick Check:
if(condition) with parentheses [OK]
- Missing parentheses in if condition
- Using 'then' keyword in JavaScript
- Incorrect function call syntax
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] ]; solveSudoku(board); console.log(board);
Solution
Step 1: Understand solveSudoku function purpose
solveSudoku uses backtracking to fill all empty cells correctly, solving the puzzle.Step 2: Check console.log output after solving
After solveSudoku runs, the board is fully solved with no zeros remaining.Final Answer:
A fully solved Sudoku board with no zeros -> Option CQuick Check:
Backtracking solves all cells [OK]
- Expecting original board unchanged
- Thinking partial fill is final
- Assuming function is undefined
function isSafe(board, row, col, num) {
for (let x = 0; x < 9; x++) {
if (board[row][x] === num || board[x][col] === num) {
return false;
}
}
// Missing 3x3 box check
return true;
}
What is the main problem here?Solution
Step 1: Analyze row and column checks
The code correctly checks if num exists in the same row or column.Step 2: Identify missing 3x3 subgrid check
Sudoku rules require checking the 3x3 box; this code does not do that, causing errors.Final Answer:
It does not check the 3x3 subgrid for duplicates -> Option BQuick Check:
3x3 box check missing = bug [OK]
- Ignoring 3x3 box validation
- Thinking loop variable causes error
- Assuming early return is wrong
Solution
Step 1: Understand MRV heuristic
MRV chooses the empty cell with the fewest valid options to reduce branching and speed up solving.Step 2: Compare with other options
Random guessing and depth-first search without pruning are less efficient; sorting rows does not help solving.Final Answer:
Heuristic with Minimum Remaining Value (MRV) strategy -> Option AQuick Check:
MRV = choose cell with fewest options [OK]
- Confusing random guessing with heuristics
- Ignoring pruning benefits
- Thinking sorting helps solving
Ready to prove it?
Pass the test and this pattern moves unknown
