← Questions
Valid Sudoku in one pass
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's coding round, last asked Aug 2025. It tests hashing and matrices.
Given a partly filled 9×9 Sudoku board, decide whether the filled cells break any rule: no digit may repeat within a row, a column or one of the nine 3×3 boxes. Empty cells are ignored, and the board does not have to be solvable. Check it in a single pass over the 81 cells.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
is_valid_sudoku(board: list[list[str]]) -> bool— each cell is"1"–"9"or"."for empty.
Example
[["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"]]
-> True
The same board with the top-left "5" changed to "8"
-> False # two 8s in column 0 (rows 0 and 3), and two in the top-left box
Constraints
- The board is exactly 9×9; cells hold
"1"–"9"or".". - One scan over the cells; stop at the first conflict.
Clarifying questions
- Do you only check the current entries, or also whether the board can be solved?
- Can the input be malformed (wrong size, other characters), and what should happen then?
- Is early exit on the first conflict fine?
What a strong answer covers
- The box index
(row // 3) * 3 + col // 3. - One bitmask per row, column and box (27 integers), or 27 hash sets; test the bit before setting it, and return
Falsethe moment it is already set. - O(81) time and O(1) space, and why bitmasks beat sets here.
- Edge cases: an empty board, a full valid board, a duplicate only in a box.
Follow-ups
- Solve the board (backtracking with the same masks to pick candidates fast).
- Generalize to
n² × n²boards. - Report every conflicting cell instead of stopping at the first.
Where it's asked
- Amazon · Coding · Last asked Aug 202555 min · Usually two rounds; one is often an object-oriented design built in steps · No AI
What it tests
hashing · matrices
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
