Validate 9x9 grid in one pass
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Onsite
Quick Answer: 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.
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
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']],)
Expected Output: True
Explanation: The canonical valid LeetCode example board. No row, column, or 3x3 box contains a duplicate digit, so the configuration is valid.
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']],)
Expected 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.
Input: ([['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.']],)
Expected Output: True
Explanation: Edge case: a fully empty board has no filled cells and therefore no conflicts, so it is trivially valid.
Input: ([['5','5','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.']],)
Expected Output: False
Explanation: Two 5s in the same row (and same 3x3 box) at positions (0,0) and (0,1) — a row duplicate makes the board invalid.
Input: ([['5','.','.','.','.','.','.','.','.'],['5','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.']],)
Expected Output: False
Explanation: Two 5s in the same column at (0,0) and (1,0) — a column duplicate makes the board invalid.
Hints
- Map a 3x3 box to an index with box = (row // 3) * 3 + (col // 3); cells in the same box share this index.
- Keep one bitmask integer per row, per column, and per box. For digit d (0-8) the bit is 1 << d.
- 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.
- Empty cells ('.') are skipped entirely; they never participate in a conflict.