Validate a 9×9 grid under constraints
Company: Verkada
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates the ability to perform constraint validation on a fixed-size 9×9 grid, testing skills in matrix/array handling, duplicate detection and use of simple data structures for correctness checking.
Constraints
- board always has exactly 9 rows and 9 columns
- Each board[r][c] is either '.' or a character from '1' to '9'
- Only the currently filled cells need to be validated
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: No filled digit repeats in any row, column, or 3×3 sub-grid.
Input: ([['5','3','.','.','7','.','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: Row 0 contains the digit '7' twice, so the board is invalid.
Input: ([['5','3','.','.','7','.','.','.','.'],['6','.','.','1','9','5','.','.','.'],['.','9','8','.','.','.','.','6','.'],['8','.','.','.','6','.','.','.','3'],['4','.','.','8','.','3','.','.','1'],['7','.','.','.','2','.','.','.','6'],['.','6','.','.','.','.','2','8','.'],['8','.','.','4','1','9','.','.','5'],['.','.','.','.','8','.','.','7','9']],)
Expected Output: False
Explanation: Column 0 contains the digit '8' twice, so the board is invalid.
Input: ([['5','3','6','.','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: The top-left 3×3 sub-grid contains the digit '6' twice, so the board is invalid.
Input: ([['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.'],['.','.','.','.','.','.','.','.','.']],)
Expected Output: True
Explanation: An empty 9×9 board has no rule violations.
Hints
- Use separate hash sets to track which digits have already appeared in each row, column, and 3×3 box.
- A cell at row r and column c belongs to box index (r // 3) * 3 + (c // 3).