Skip to main content

43. Valid Sudoku

mediumAsked at Snowflake

Determine whether a partially-filled 9x9 Sudoku board is valid. Snowflake uses this to test multi-key uniqueness checking — the same pattern used to enforce composite unique constraints in their schema validation.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Snowflake loops.

  • Glassdoor (2025-Q4)Snowflake new-grad onsite uses this to set up multi-column constraint discussion.
  • LeetCode Discuss (2025-10)Reported at Snowflake SDE-I screens.

Problem

Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: each row must contain the digits 1-9 without repetition; each column must contain the digits 1-9 without repetition; each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.

Constraints

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

Examples

Example 1

Input
9x9 valid partial board
Output
true

Example 2

Input
9x9 board with two 8s in the same row
Output
false

Approaches

1. Three separate passes

Check rows, then cols, then 3x3 boxes — three iterations.

Time
O(81) = O(1)
Space
O(1)
// outline: for each row check duplicates; same for cols; same for boxes.
// Verbose but straightforward.

Tradeoff: Works but verbose.

2. Single pass with composite keys (optimal)

One pass over the board. Use a single Set with keys like 'row-0-5', 'col-3-5', 'box-1-2-5'. Return false on any duplicate insert.

Time
O(81) = O(1)
Space
O(81) = O(1)
function isValidSudoku(board) {
  const seen = new Set();
  for (let r = 0; r < 9; r++) {
    for (let c = 0; c < 9; c++) {
      const v = board[r][c];
      if (v === '.') continue;
      const rowKey = `r${r}-${v}`;
      const colKey = `c${c}-${v}`;
      const boxKey = `b${Math.floor(r / 3)}-${Math.floor(c / 3)}-${v}`;
      if (seen.has(rowKey) || seen.has(colKey) || seen.has(boxKey)) return false;
      seen.add(rowKey);
      seen.add(colKey);
      seen.add(boxKey);
    }
  }
  return true;
}

Tradeoff: Single pass, encodes all three constraints into composite keys. Generalizes to N-dimensional uniqueness constraints.

Snowflake-specific tips

Snowflake interviewers want the composite-key version because it generalizes cleanly. Bonus signal: connect to multi-column unique constraint enforcement — when Snowflake validates UNIQUE(col1, col2, col3) it hashes the tuple, and the lookup pattern is identical.

Common mistakes

  • Forgetting to skip '.' cells.
  • Off-by-one in box index calculation (use floor division).
  • Returning true at the end without checking that every constraint was actually checked.

Follow-up questions

An interviewer at Snowflake may pivot to one of these next:

  • Sudoku Solver (LC 37) — generalize to actually fill the board.
  • How does Snowflake validate UNIQUE constraints?
  • Generalize to NxN with N = k^2 boxes.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why prefix the keys with 'r', 'c', 'b'?

Without prefixes, 'row 2, value 5' could collide with 'col 2, value 5'. Prefixes distinguish the three axes.

Why is this O(1)?

The board is fixed at 9x9 = 81 cells. The algorithm runs at most 81 iterations regardless of input. We say O(1) when the input size is bounded.

Practice these live with InterviewChamp.AI

Drill Valid Sudoku and other Snowflake interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →