KatInterview

Validate Partially Filled Sudoku Grid

Arrays & HashingAuto-graded · 20 test cases
MediumAuto-gradedArrays & Hashinghash-setconstraint-checkingmatrix

Last updated

Tests ability to check constraint satisfaction across multiple overlapping groups using efficient duplicate detection.

You receive a 9 x 9 Sudoku grid where some cells contain digits and others are blank. Your task is to verify whether the current state obeys Sudoku's placement rules.

Write a function that returns True if the grid satisfies all of the following conditions, and False otherwise:

  • Each row contains no repeated digits among its filled cells
  • Each column contains no repeated digits among its filled cells
  • Each of the nine 3 x 3 subgrids contains no repeated digits among its filled cells

The nine subgrids are formed by dividing the board into a 3×3 arrangement: rows 0-2, 3-5, 6-8 crossed with columns 0-2, 3-5, 6-8.

Important: You are only checking the current state for conflicts. Blank cells never violate any rule. You do not need to determine whether the puzzle has a valid completion.

Constraints

  • The input is always a 9 x 9 grid: len(board) == 9 and len(board[i]) == 9
  • Each cell contains either a digit character "1" through "9", or "." representing a blank cell
  • The grid may be completely empty, partially filled, or completely filled

Examples

Example 1

Input: validate_sudoku([['2', '.', '.', '5', '.', '.', '1', '.', '.'], ['.', '3', '.', '.', '9', '.', '.', '6', '.'], ['.', '.', '7', '.', '.', '4', '.', '.', '8'], ['9', '.', '.', '6', '.', '.', '3', '.', '.'], ['.', '8', '.', '.', '7', '.', '.', '4', '.'], ['.', '.', '5', '.', '.', '1', '.', '.', '2'], ['4', '.', '.', '8', '.', '.', '7', '.', '.'], ['.', '1', '.', '.', '2', '.', '.', '5', '.'], ['.', '.', '6', '.', '.', '3', '.', '.', '9']])

Output: True

Example 2

Input: validate_sudoku([['2', '.', '.', '5', '.', '.', '1', '.', '.'], ['7', '3', '.', '.', '9', '.', '.', '6', '.'], ['.', '.', '7', '.', '.', '4', '.', '.', '8'], ['9', '.', '.', '6', '.', '.', '3', '.', '.'], ['.', '8', '.', '.', '7', '.', '.', '4', '.'], ['.', '.', '5', '.', '.', '1', '.', '.', '2'], ['4', '.', '.', '8', '.', '.', '7', '.', '.'], ['.', '1', '.', '.', '2', '.', '.', '5', '.'], ['.', '.', '6', '.', '.', '3', '.', '.', '9']])

Output: False

Hints

Hint 1

Track which digits appear in each row, column, and subgrid independently to detect duplicates efficiently

Hint 2

Use integer division to map any cell position to its subgrid: both the row and column indices can be divided by 3

Hint 3

A single set with composite keys can replace multiple separate data structures if you tag each entry appropriately