Valid Sudoku
Filed under Amazon
Free read 3 of 3Use three arrays of sets (or boolean arrays) to track seen numbers for rows, columns, and 3x3 blocks.
The question
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the rules: each row, column, and 3x3 sub-box must contain digits 1-9 without repetition.
Constraints
- Board is always 9x9.
- Empty cells are represented by '.'
- Time complexity O(1) since board size is fixed.
Examples
- Input
- 9x9 grid
- Output
- True/False
- Why
- Check sets for each row, column, and block.
Approach
Use three arrays of sets (or boolean arrays) to track seen numbers for rows, columns, and 3x3 blocks.
Iterate through each cell (r, c). If cell is not '.', calculate block index: (r//3)*3 + (c//3).
Check if digit exists in corresponding row, col, or block set. If yes, return False. Else, add to sets.
Tips
- What they are testing: Test an empty board and a full board with one duplicate.
- Common mistake: Using a single set for the whole board instead of separate sets for rows, columns, and boxes.
Related Amazon questions
Collected from public candidate posts and coding forums. Updated Nov 2026.