Sudoku Solver - Problem

Imagine being handed a partially completed Sudoku puzzle - one of the world's most beloved logic games! Your mission is to write an intelligent program that can automatically solve any valid Sudoku puzzle by filling in the empty cells (marked with '.').

A complete Sudoku solution must follow three sacred rules:

  • Row Rule: Each of the digits 1-9 must appear exactly once in each row
  • Column Rule: Each of the digits 1-9 must appear exactly once in each column
  • Box Rule: Each of the digits 1-9 must appear exactly once in each of the 9 3ร—3 sub-boxes

Your algorithm needs to be smart enough to use logical deduction and backtracking to find the unique solution, just like a human solver would - but much faster!

Goal: Transform an incomplete 9ร—9 grid into a completely solved Sudoku puzzle.

Input & Output

example_1.py โ€” Basic Sudoku Puzzle
$ Input: [ ["5","3",".",".","7",".",".",".","."], ["6",".",".","1","9","5",".",".","."], [".","9","8",".",".",".",".","6","."], ["8",".",".",".","6",".",".",".","3"], ["4",".",".","8",".","3",".",".","1"], ["7",".",".",".","2",".",".",".","6"], [".","6",".",".",".",".","2","8","."], [".",".",".","4","1","9",".",".","5"], [".",".",".",".","8",".",".","7","9"] ]
โ€บ Output: [ ["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"] ]
๐Ÿ’ก Note: This is a typical Sudoku puzzle with a unique solution. The algorithm fills in the missing numbers while ensuring each row, column, and 3x3 box contains all digits 1-9 exactly once.
example_2.py โ€” Nearly Complete Puzzle
$ Input: [ ["5","3","4","6","7","8","9","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","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"] ]
โ€บ Output: [ ["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"] ]
๐Ÿ’ก Note: This puzzle has only one empty cell. The constraint propagation approach would immediately identify that only '2' can go in position [0][8], making this solve in O(1) time.
example_3.py โ€” Challenging Puzzle
$ Input: [ [".",".",".",".",".",".","6","8","."], [".",".",".",".","4",".",".",".","."], [".",".",".",".",".",".",".",".","."], [".",".",".",".",".",".",".",".","."], [".",".",".",".",".",".","4",".","."], [".",".",".",".",".",".",".",".","."], [".",".",".",".",".",".",".",".","."], [".",".",".",".","2",".",".",".","."], [".",".",".",".",".",".",".",".","."] ]
โ€บ Output: [ ["2","7","4","3","9","1","6","8","5"], ["1","3","6","8","4","5","9","2","7"], ["8","9","5","6","7","2","3","1","4"], ["6","1","8","9","5","3","7","4","2"], ["3","5","9","1","6","7","4","9","8"], ["7","4","2","4","8","9","1","5","6"], ["5","6","1","2","3","4","8","7","9"], ["9","8","3","7","2","6","5","6","1"], ["4","2","7","5","1","8","2","3","3"] ]
๐Ÿ’ก Note: This sparse puzzle with very few given numbers represents a challenging case where backtracking becomes essential. The MCV heuristic helps by choosing cells with fewer possibilities first.

Constraints

  • board.length == 9
  • board[i].length == 9
  • board[i][j] is a digit 1-9 or '.'
  • It is guaranteed that the input board has only one solution

Visualization

Tap to expand
Smart Sudoku Solving Strategy537only option{2,4}{1,2,4,6}๐ŸŽฏ Strategy Overview1. Track Constraintsโ€ข Maintain sets of possible valuesโ€ข Update on each placement2. Propagate Obviousโ€ข Fill cells with one option ๐ŸŸขโ€ข Repeat until no more obvious3. Choose Smartlyโ€ข Pick cell with fewest options ๐ŸŸกโ€ข Avoid hard cells ๐Ÿ”ด until later4. Backtrack Efficientlyโ€ข Restore constraint stateโ€ข Try next possibilityโšก Performance ImpactBasic backtracking: 9^81 โ‰ˆ 10^77Smart approach: 9^k where k โ‰ช 81Typical speedup: 1000x - 1,000,000x
Understanding the Visualization
1
Setup Constraints
Initialize tracking sets for each row, column, and 3ร—3 box with digits 1-9, then remove already placed numbers
2
Constraint Propagation
Find cells with only one possible value and fill them immediately - like solving obvious clues first
3
Most Constrained Variable
Among remaining empty cells, choose the one with fewest possible values to branch on
4
Intelligent Backtrack
Try each possibility, update constraints, and recursively solve. Backtrack with state restoration if stuck
Key Takeaway
๐ŸŽฏ Key Insight: Combining constraint propagation with the Most Constrained Variable heuristic transforms an exponential search into a highly efficient algorithm that solves most Sudoku puzzles in milliseconds.
Asked in
Google 35 Amazon 28 Apple 22 Microsoft 18
425.0K Views
Medium Frequency
~25 min Avg. Time
8.7K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen