Sudoku Solver

hard

Fill every empty cell of a 9x9 grid so the finished board is a valid Sudoku.

A completed board must satisfy all three rules:

  1. Each row contains the digits 1-9 exactly once.
  2. Each column contains the digits 1-9 exactly once.
  3. Each of the nine 3x3 sub-boxes contains the digits 1-9 exactly once.

Empty cells are marked with the character '.'. Fill them in place; the input is guaranteed to have exactly one valid solution.

The board is given as a 9x9 grid of single characters — each is either a digit '1'-'9' or '.'.

Hints

Only the empty cells are decisions — the clues never move. Can you visit the blanks one at a time and try filling each?
For a given blank, what does a digit have to avoid? Exactly three groups: its row, its column, and its 3x3 box.
Keep a 'used digits' set for each row, column, and box so 'is this legal?' becomes O(1). A single integer's bits make a perfect nine-slot set, flipped on placement and un-flipped on backtrack.

Common doubts

Use b = (r / 3) * 3 + c / 3 with integer division. It maps every cell to one of the boxes 0-8. The common bug is r / 3 + c / 3, which collapses different boxes onto the same index.
Sudoku Solver mutates the board in place — that is the whole point. Fill the '.' cells directly; returning the same reference is fine.
Erase it back to '.' (and clear any state you set) before trying the next digit. Skipping that undo is the single most common source of wrong answers.

Interview follow-ups

Pick the most-constrained blank first (minimum-remaining-values): always fill the cell with the fewest legal digits, which prunes the search tree dramatically before you ever branch.
Do not stop at the first completed board — keep searching, incrementing a counter, and early-exit once the count exceeds one. A valid Sudoku must reach exactly one.

Fun facts

  • The minimum number of clues that can pin down a unique 9x9 solution is 17 — proven in 2012 after an exhaustive computer search; no valid 16-clue puzzle exists.
  • The same row/column/box bitmask trick powers N-Queens, graph coloring, and exact-cover solvers like Knuth's Dancing Links — Sudoku is just exact cover in disguise.

Asked at

AmazonMicrosoftGoogleAppleUber
Frequently Sometimes Occasionally
Example 1
Input: board = [["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"]]
Every row, every column, and every 3x3 box ends up holding the digits 1-9 exactly once. It is the only board that fits the given clues.
Constraints

- board.length == 9 - board[i].length == 9 - Each board[i][j] is a digit 1-9 or the character '.' marking an empty cell. - The puzzle is guaranteed to have exactly one solution.

Solve this problem →