Bird
Raised Fist0
Interview PrepbacktrackinghardAmazonFacebookGoogleSnap

Sudoku Solver

Choose your preparation mode3 modes available
Steps
setup

Initialize constraint sets and empty cells list

Scan the board to fill sets for rows, columns, and boxes with existing digits, and collect coordinates of empty cells.

💡 This step sets up the data structures that track which digits are already used in each row, column, and box, enabling quick validity checks later.
Line:rows = [set() for _ in range(9)] cols = [set() for _ in range(9)] boxes = [set() for _ in range(9)] empties = []
💡 Tracking constraints upfront allows the solver to efficiently check candidate digits without scanning the board repeatedly.
📊
Sudoku Solver - Watch the Algorithm Execute, Step by Step
Watching each choice and backtrack visually reveals how the algorithm prunes invalid paths early and efficiently fills the Sudoku grid, which is hard to grasp from code alone.
Step 1/10
·Active fillAnswer cell
entering
Call Path (current → root)
backtrack()
choosing
Call Path (current → root)
backtrack()
Remaining
124
entering
Call Path (current → root)
backtrack()
backtrack()
Partial: [(0,2)=1]
choosing
Call Path (current → root)
backtrack()
backtrack()
Remaining
124
Partial: [(0,2)=1]
pruning
Call Path (current → root)
backtrack()
backtrack()
Tried
1
Remaining
24
Partial: [(0,2)=1]
Digit '1' conflicts with column 3
entering
Call Path (current → root)
backtrack()
backtrack()
backtrack()
Partial: [(0,2)=1, (0,3)=2]
backtracking
Call Path (current → root)
backtrack()
backtrack()
backtrack()
Tried
2
Partial: [(0,2)=1]
entering
Call Path (current → root)
backtrack()
backtrack()
backtrack()
Partial: [(0,2)=1, (0,3)=4]
choosing
Call Path (current → root)
...
backtrack()
backtrack()
backtrack()
Partial: [(0,2)=1, (0,3)=4, ...]
backtracking
Call Path (current → root)
backtrack()
...
backtrack()
backtrack()
backtrack()
Found 1: [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]

Key Takeaways

The heuristic of choosing the most constrained empty cell first drastically reduces the search space.

This insight is difficult to see from code alone because the sorting and candidate counting happen implicitly and recursively.

Backtracking systematically explores all candidate digits for each cell, undoing invalid choices to find the unique solution.

Watching the recursion tree reveals the depth-first search nature and how backtracking prunes invalid paths early.

Constraint sets for rows, columns, and boxes enable quick validity checks, avoiding expensive board scans.

This optimization is subtle in code but critical for performance and is clearly visible in the visualization as pruning decisions.