The "Sudoku Solver" problem asks you to write a program that fills in the empty cells of a 9x9 Sudoku board. A valid Sudoku solution must satisfy three conditions: each of the digits 1-9 must occur exactly once in each row, exactly once in each column, and exactly once in each of the nine 3x3 sub-boxes of the grid. Empty cells are typically denoted by a character like '.'.
This is a quintessential Hard-level backtracking problem asked by nearly every major tech company (Apple, Uber, Google, etc.). It tests a candidate's ability to implement a recursive search with state management. It requires careful handling of constraints and efficient "is valid" checks. Solving Sudoku isn't just about the puzzle; it's about demonstrating that you can navigate a large search space and prune unnecessary paths efficiently.
The pattern used is Backtracking. The algorithm tries to fill an empty cell with a digit from '1' to '9'. For each digit, it checks if the placement is valid (no conflicts in row, column, or 3x3 box). If it's valid, the algorithm moves to the next empty cell. If it reaches a point where no digit can be placed, it "backtracks" by clearing the current cell and trying the next digit in the previous cell. This depth-first search continues until the board is completely filled.
Imagine a board where the first empty cell is at (0, 2).
Practice backtracking with smaller grids first (like a 4x4 Sudoku) to get the logic right. Focus on how to represent the constraints. Can you use a bitmask to check if a number is present in a row in O(1)? Small optimizations like these can turn a slow backtracking solution into a highly efficient one.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Valid Sudoku | Medium | Solve | |
| Set Matrix Zeroes | Medium | Solve | |
| Path with Maximum Gold | Medium | Solve | |
| Sparse Matrix Multiplication | Medium | Solve | |
| Flip Columns For Maximum Number of Equal Rows | Medium | Solve |