Sudoku Board Audit

MediumArrays & Hashing

Problem

A puzzle app lets players type digits into a 9×9 Sudoku grid and wants to flag a mistake as soon as one is visible. A board is consistent when no digit repeats within any row, any column, or any of the nine 3×3 boxes.

Given board, where each cell is "1"–"9" or "." for an empty cell, return true if the filled cells are consistent. You do not need to decide whether the puzzle can be completed.

Example 1

Input:
board = [["1",".",".","4",".",".","7",".","."], [".","5",".",".","8",".",".","2","."], [".",".","9",".",".","3",".",".","6"], ["2",".",".","5",".",".","8",".","."], [".","6",".",".","9",".",".","3","."], [".",".","1",".",".","4",".",".","7"], ["3",".",".","6",".",".","9",".","."], [".","7",".",".","1",".",".","4","."], [".",".","2",".",".","5",".",".","8"]]
Output:
true

Every row, column and box holds distinct digits.

Example 2

Input:
The same board with "9" typed at row 0, column 1.
Output:
false

Row 0 and column 1 are still fine, but the top-left box now holds two 9s (rows 0 and 2).

Constraints

  • board is 9×9.
  • Each cell is a digit "1"–"9" or ".".

Hint

Show the first hint

Each filled cell belongs to exactly three groups: its row, its column and its box.

Walkthrough

How to approach it

01Clarify before coding

  • Must I check that the puzzle is solvable? No, only that the visible digits do not clash.
  • Is the board always 9×9 with valid characters? Yes.
  • Do empty cells matter? No, skip them.

02Brute force

Check each row, then each column, then each box separately, each time scanning its 9 cells with a fresh set (or comparing every pair of cells in a group).

Cost: Three passes of 81 cells with sets is already O(81); the pairwise version is 27 groups × 36 pairs. The size is fixed, so the interviewer is judging clarity, not asymptotics.

The rest of the walkthrough is in the app

The full walkthrough (key insight, step-by-step, dry run, complexity, edge cases, pitfalls, follow-ups) and solutions in Python and JavaScript are in the app, next to an editor and a terminal that runs your code against the tests.

This problem is part of Pro.