A cool algorithm to check a Sudoku field?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Checking whether a Sudoku board is valid is a classic constraint-validation problem. You need to ensure rows, columns, and three-by-three boxes contain no duplicate digits. A clean algorithm can validate a board in one pass with constant extra space relative to board size.
Sudoku Validation Rules
For a standard nine-by-nine board:
- each row can contain digits one to nine at most once
- each column can contain digits one to nine at most once
- each three-by-three box can contain digits one to nine at most once
Empty cells are allowed in partial puzzles and should be ignored during duplicate checks.
Data Representation Choices
Common board encodings:
- strings with dot for empty cells
- integers with zero for empty cells
- characters with digit symbols
The validation strategy is same regardless of representation, as long as you normalize values consistently.
One-Pass Set-Based Validator
A practical and readable approach uses three arrays of sets:
- one set per row
- one set per column
- one set per box
This solution is easy to reason about and fits most interview and production validation needs.
Bitmask Variant for Compactness
A faster and memory-compact variant tracks seen digits with bitmasks.
Bitmasks are useful in performance-sensitive solvers and backtracking engines.
Complexity Analysis
For a fixed nine-by-nine board, runtime is effectively constant in practical terms. In generalized form for board size n by n, this validator runs in O of n squared time with O of n auxiliary structures.
The key win is single-pass validation with immediate failure on first conflict.
Integrating with Sudoku Solvers
Validation is usually a guard step before running a full solver. If input puzzle is invalid, you should fail early and avoid expensive search.
Typical flow:
- parse input board
- validate structure and constraints
- run solver only when valid
- verify solved output consistency
This separation simplifies debugging and improves solver reliability.
Testing Strategy
Useful tests include:
- valid partial board
- duplicate in one row
- duplicate in one column
- duplicate in one box
- invalid character input
You can also generate random valid complete boards and remove cells to stress-test parser and validator combinations.
Common Pitfalls
A common pitfall is calculating wrong box index and mixing cells between neighboring boxes. Another is treating empty markers as regular values and triggering false duplicates. Teams also often skip input normalization and allow unexpected symbols, causing inconsistent behavior later in solver code. Finally, validating only rows and columns while forgetting box constraints results in boards incorrectly marked valid.
Summary
- Sudoku validation checks rows, columns, and boxes for duplicates.
- A one-pass set-based solution is clear and robust.
- Bitmask approach offers compact high-performance validation.
- Validate puzzle before solving to fail fast on bad input.
- Cover row, column, box, and input-format edge cases in tests.

