By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"Sudoku Solver is the ultimate test of backtracking mastery—it appears in 1 out of every 3 onsite interviews at top-tier companies like Google, Meta, and Amazon. Nail it, and you prove you can handle constraint propagation, recursion, and pruning—skills that separate L4 from L5 engineers."
Before tackling Sudoku Solver, you must understand: 1. Backtracking (DFS with pruning) – Recursively explore possibilities, undoing invalid choices. 2. 2D Array Traversal – Efficiently check rows, columns, and 3x3 subgrids. 3. Hash Sets / Bitmasking – Track used numbers in O(1) time.
int
1 << num
Mental checklist for every Sudoku problem:
'.'
'1'-'9'
Output: Modify the board in-place (no return value).
Choose a Cell to Fill
If no empty cells, the board is solved.
Generate Valid Candidates
For the chosen cell, check:
Recursive Backtracking
For each candidate:
True
Base Case
If no candidates work, return False.
False
Optimizations (Optional but Recommended)
Problem: Solve a 9x9 Sudoku board with backtracking.
1-9
def solveSudoku(board): def is_valid(row, col, num): # Check row for i in range(9): if board[row][i] == num: return False # Check column for i in range(9): if board[i][col] == num: return False # Check 3x3 subgrid start_row, start_col = 3 (row // 3), 3 (col // 3) for i in range(3): for j in range(3): if board[start_row + i][start_col + j] == num: return False return True def backtrack(): for row in range(9): for col in range(9): if board[row][col] == '.': for num in map(str, range(1, 10)): if is_valid(row, col, num): board[row][col] = num if backtrack(): return True board[row][col] = '.' # Backtrack return False # No valid number found return True # All cells filled backtrack()
O(9^(n))
n
O(n)
Problem: Solve Sudoku faster by tracking used numbers with bitmasks.
rows
cols
subgrids
rows[row] |= (1 << num)
def solveSudoku(board): rows = [0] 9 cols = [0] 9 subgrids = [0] 9 # Initialize bitmasks for i in range(9): for j in range(9): if board[i][j] != '.': num = int(board[i][j]) mask = 1 << num rows[i] |= mask cols[j] |= mask subgrids[(i // 3) 3 + (j // 3)] |= mask def backtrack(): for i in range(9): for j in range(9): if board[i][j] == '.': for num in range(1, 10): mask = 1 << num subgrid_idx = (i // 3) 3 + (j // 3) if not (rows[i] & mask) and not (cols[j] & mask) and not (subgrids[subgrid_idx] & mask): board[i][j] = str(num) rows[i] |= mask cols[j] |= mask subgrids[subgrid_idx] |= mask if backtrack(): return True # Backtrack board[i][j] = '.' rows[i] ^= mask cols[j] ^= mask subgrids[subgrid_idx] ^= mask return False return True backtrack()
O(1)
O(9)
Problem: Count all possible solutions for a given Sudoku board (follow-up question).
0
1
def countSolutions(board): rows = [0] 9 cols = [0] 9 subgrids = [0] 9 # Initialize bitmasks for i in range(9): for j in range(9): if board[i][j] != '.': num = int(board[i][j]) mask = 1 << num rows[i] |= mask cols[j] |= mask subgrids[(i // 3) 3 + (j // 3)] |= mask def backtrack(): for i in range(9): for j in range(9): if board[i][j] == '.': count = 0 for num in range(1, 10): mask = 1 << num subgrid_idx = (i // 3) 3 + (j // 3) if not (rows[i] & mask) and not (cols[j] & mask) and not (subgrids[subgrid_idx] & mask): board[i][j] = str(num) rows[i] |= mask cols[j] |= mask subgrids[subgrid_idx] |= mask count += backtrack() # Backtrack board[i][j] = '.' rows[i] ^= mask cols[j] ^= mask subgrids[subgrid_idx] ^= mask return count return 1 # All cells filled return backtrack()
"Alright, let’s lock this in. Sudoku Solver is all about backtracking with pruning. Here’s the playbook:
You’ve got this. Now go solve that Sudoku like a pro!
Now go ace that interview! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.