Design a Sudoku Validator with Custom Rules

Difficulty: EASYCategory: LLDFrequency: 1Asked On 2024-09-25

Problem Statement

Design and implement a system to validate a Sudoku board, with support for custom rule extensions.


🎯 Functional Requirements

1. validateBoard(char[][] board) → boolean

•Validates a standard 9x9 Sudoku board.
•Returns true if the board is partially or fully valid according to Sudoku rules:
•Each row must contain digits 1–9 without repetition.
•Each column must contain digits 1–9 without repetition.
•Each 3x3 sub-grid must contain digits 1–9 without repetition.
•Empty cells are denoted by '.' and can be ignored.


🔁 Twist / Follow-Up Extensions

🔸 2. Support custom validation rules, such as:

a. Knight’s Move Rule

•A digit cannot appear again in a cell that is a knight’s move away (like in chess).

b. King’s Move Rule

•A digit cannot appear in any adjacent cell (including diagonals).

c. Diagonal Sudoku

•Digits 1–9 must also be unique on both main diagonals.


🧱 Suggested Design

interface SudokuRule

1java 2CopyEdit 3boolean isValid(char[][] board, int row, int col, char value);

class SudokuValidator

1java 2CopyEdit 3List<SudokuRule> rules; 4 5boolean validateBoard(char[][] board); 6void addRule(SudokuRule rule);

Built-in Rules:

•RowRule, ColumnRule, BoxRule → classic Sudoku rules
•Optional: KnightMoveRule, KingMoveRule, DiagonalRule

🧪 Sample Input

1plaintext 2CopyEdit 3[ 4 ['5','3','.','.','7','.','.','.','.'], 5 ['6','.','.','1','9','5','.','.','.'], 6 ['.','9','8','.','.','.','.','6','.'], 7 ... 8]

With:

•Classic Sudoku Rules ✅
•Knight Rule enabled ❌ (if violates a knight move)


🔐 Constraints

•Board size is always 9x9
•All input values are digits '1' to '9' or '.'
•Multiple rules may be enabled simultaneously
•Should be easily extensible


✅ Evaluation Criteria

•Correct validation logic
•Extensibility via rule interfaces or strategy pattern
•Clean separation of concerns (rules vs engine)
•Efficiency — no redundant traversals


✨ Optional Enhancements

•Visual debug mode: show which rule failed where
•Validate fully solved vs partially filled boards
•Add support for N×N Sudoku (e.g., 16x16)
•Suggest corrections or next valid moves


Solution

Comments