MediumArrayHash TableMatrixUpdated Sep 2026

36. Valid Sudoku

Asked at Apple

Difficulty

Medium

Time

O(1)

Space

O(1)

Pattern

Array

Companies

1

Problem

You are given a 9 x 9 Sudoku board. Each cell holds a digit from '1' to '9', or '.' if the cell is empty. Decide whether the board is valid.

A board is valid when no digit repeats inside any row, inside any column, or inside any of the nine 3 x 3 sub-boxes. Empty cells are ignored — you only check the cells that already hold a digit.

Important: a valid board does not have to be solvable. You are not asked to fill in the empty cells. You only report whether the digits that are already there break any of the three rules. Return true if nothing is broken, otherwise false.

Three rules to check

board01234567801234567853..7....6..195....98....6.8...6...34..8.3..17...2...6.6....28....419..5....8..79The top-left 3 x 3 box holds 5, 3, 6, 9, 8 — all different, sothis box is fine
A digit must be unique in its row, its column, and its 3 x 3 box — the shaded box is one of the nine boxes.

Constraints

  • ●board.length == 9
  • ●board[i].length == 9
  • ●board[i][j] is a digit 1-9 or '.'

Examples

Example 1
Inputboard = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],[
Outputtrue
WhyEvery filled digit appears only once in its row, only once in its column, and only once in its 3 x 3 box. So no rule is broken and the answer is true.
Example 2
Inputboard = [["8","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],[
Outputfalse
WhyThis board is the first example with the top-left cell changed from '5' to '8'. Now the top-left 3 x 3 box holds two 8's (at row 0, col 0 and row 3, col 0), so the box rule is broken and the answer is false.

Asked At

1 companies
CompanyDifficulty
AppleMediumView all Apple questions →

How to Think About It

1

Brute force: for each cell, check all cells in the same row, column, and box for duplicates.

2

Use three hash sets per row, column, and box to track seen digits.

3

Iterate through all 81 cells once, inserting each digit into the appropriate sets.

4

For boxes, compute the box index as (row/3)*3 + col/3.

5

All checks are done in a single pass: O(81) time and O(81) space.

Optimal Approach

Maintain 27 sets: 9 for rows, 9 for columns, and 9 for 3x3 boxes. For each filled cell, check if the digit is already in the corresponding row, column, or box set. If yes, return false. Otherwise, add the digit to all three sets. If all 81 cells pass, return true. This is O(81) time and O(81) space.

One pass with 27 sets

board01234567801234567853..7....6..195....98....6.8...6...34..8.3..17...2...6.6....28....419..5....8..79Scan cells left to right, top to bottom; skip every '.' cellboard01234567801234567883..7....6..195....98....6.8...6...34..8.3..17...2...6.6....28....419..5....8..79Here '8' is already in the top-left box set, so we stop andreturn falsebox index = (row // 3) * 3 + col // 3if digit in row set or col set or box set: return falseelse add it to all three sets
For each filled cell, look it up in its row set, column set and box set, then add it to all three.
ComplexityTime O(1)Space O(1)The board is always 81 cells, so the single pass costs a fixed 81 steps and the 27 sets hold at most 81 digits in total.

What Trips People Up in Real Interviews

1

Clarify: only filled cells matter; dots are ignored.

2

Hash sets can be replaced with bitmasks for O(1) extra space.

3

Be careful with box indexing: (row/3)*3 + col/3 maps each cell to its 3x3 box.

4

Interviewers may ask to also solve the board — that is a separate backtracking problem.

5

Confirm whether the input is guaranteed to be a 9x9 board.

Solution Code

O(1) time
class Solution:
    def isValidSudoku(self, board: list[list[str]]) -> bool:
        rows = [set() for _ in range(9)]
        cols = [set() for _ in range(9)]
        boxes = [set() for _ in range(9)]
        for r in range(9):
            for c in range(9):
                val = board[r][c]
                if val == '.':
                    continue
                b = (r // 3) * 3 + c // 3
                if val in rows[r] or val in cols[c] or val in boxes[b]:
                    return False
                rows[r].add(val)
                cols[c].add(val)
                boxes[b].add(val)
        return True

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Valid Sudoku problem?

Determine if a 9x9 Sudoku board is valid according to the rules: each row, column, and 3x3 sub-box must contain digits 1-9 without repetition. The board may be partially filled; you only need to validate the existing entries, not whether it is solvable.

How do you solve Valid Sudoku?

The optimal approach runs in O(1) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Valid Sudoku?

Valid Sudoku is asked at Apple. It is a medium difficulty problem.

What are common mistakes on Valid Sudoku?
  • Clarify: only filled cells matter; dots are ignored.
  • Hash sets can be replaced with bitmasks for O(1) extra space.
  • Be careful with box indexing: (row/3)*3 + col/3 maps each cell to its 3x3 box.
  • Interviewers may ask to also solve the board — that is a separate backtracking problem.
  • Confirm whether the input is guaranteed to be a 9x9 board.