cswork / libraryOpen practice workspace

Coding questions · Amazon

Validate 9x9 grid in one pass

This interview question evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer for Validate 9x9 grid in one pass states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Given a partially filled 9x9 board using digits '1'–'9' and '.' for empty cells, determine whether the current configuration is valid under Sudoku rules (no duplicates per row, column, or 3x3 sub-box). Provide a one-pass algorithm that detects conflicts during a single scan, describe your data structures (e.g., bitmasks or hash sets), and analyze time and space complexity.

What a Strong Answer Covers

  • A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
  • A correctness argument that explains why the algorithm covers all required cases.
  • Time and space complexity, plus at least one alternative approach when relevant.
  • Focused tests for normal cases, edge cases, and failure modes.

Follow-up Questions

  • How would the approach change if the input were streaming or too large for memory?
  • What invariants would you assert in production code?
  • Which tests would catch off-by-one, duplicate, or tie-breaking bugs?

Validate 9x9 Sudoku Board in One Pass

Given a partially filled 9x9 Sudoku board represented as a list of 9 rows, each a list of 9 single-character strings using the digits '1'-'9' and '.' for empty cells, determine whether the current configuration is valid.

A board is valid if:

  • Each row contains no duplicate digits.
  • Each column contains no duplicate digits.
  • Each of the nine 3x3 sub-boxes contains no duplicate digits.

Only the filled cells need to be checked for validity. The board does not need to be solvable. Return True if valid, False otherwise.

Solve it in a single pass over the 81 cells. Use bitmasks (one integer per row, per column, and per 3x3 box) or hash sets to detect a conflict the moment a digit repeats within any of its three regions.

Constraints

  • board.length == 9 and board[i].length == 9
  • board[i][j] is a digit '1'-'9' or '.'
  • Only the filled cells must be validated; the board need not be solvable
  • Must run in a single pass over the 81 cells

Examples

Example 1

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

Explanation: The canonical valid LeetCode example board. No row, column, or 3x3 box contains a duplicate digit, so the configuration is valid.

Example 2

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

Explanation: Changing the top-left cell from '5' to '8' puts two 8s in the first column (rows 0 and 3) — also two 8s in the top-left 3x3 box — so the board is invalid.

Hints

  1. Map a 3x3 box to an index with box = (row // 3) * 3 + (col // 3); cells in the same box share this index.
  2. Keep one bitmask integer per row, per column, and per box. For digit d (0-8) the bit is 1 << d.
  3. On each filled cell, if the bit is already set in the row, column, or box mask, you have found a duplicate — return False immediately. Otherwise set the bit in all three masks.
  4. Empty cells ('.') are skipped entirely; they never participate in a conflict.

Solution

Solution Alignment

The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.

Problem Restatement

Given a partially filled 9x9 board using digits '1'–'9' and '.' for empty cells, determine whether the current configuration is valid under Sudoku rules (no duplicates per row, column, or 3x3 sub-box). Provide a one-pass algorithm that detects conflicts during a single scan, describe your data structures (e.g., bitmasks or hash sets), and analyze time and space complexity.

Recommended Approach

Represent each cell as a state. Use BFS for minimum-distance propagation, DFS with memoization for longest monotonic paths, and careful boundary checks for simulation. Store obstacles or visited cells in sets when the grid is sparse.

Correctness

The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.

Complexity

A full grid traversal is O(mn) time and O(mn) space in the worst case. Sparse simulation is O(number_of_commands) plus obstacle storage.

Edge Cases and Tests

Empty grid, one row/column, blocked start or target, boundaries, repeated visits, and tie-breaking among equal-distance cells.