‹ DS&A interview · Socratic
DSA · Arrays & Hashing · #183

Valid Sudoku

Module 82 · difficulty 3/5·30:00starts on first keystroke

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be checked according to the following rules: 1. Each row must contain the digits `1-9` without repetition. 2. Each column must contain the digits `1-9` without repetition. 3. Each of the nine 3x3 sub-boxes must contain the digits `1-9` without repetition. The board is partially filled; empty cells are denoted by `'.'`. A board can be valid even if it is not solvable — you only validate the currently placed digits. Implement `isValidSudoku(board)` where `board` is a 9x9 array of single-character strings. Return `true` if the board is valid, otherwise `false`.

Examples
  • board = [["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"]] trueNo row, column, or 3x3 box contains a repeated digit.
  • board = [["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"]] falseThe two 8s in the top-left 3x3 box (board[0][0] and board[3][0] are both in column 0) — here board[0][0]=8 and board[3][0]=8 collide in column 0.
Constraints
  • · board.length === 9 and board[i].length === 9
  • · board[i][j] is a digit '1'-'9' or the character '.'
  • · Only filled cells are validated; an empty board is valid
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.