Skip to main content

43. Valid Sudoku

mediumAsked at Vercel

Determine if a 9x9 Sudoku board is valid. Vercel asks this for the three-constraint-set tracking — same skill they use to validate routing rules that must satisfy uniqueness across multiple dimensions.

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

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-Q4)Vercel platform onsite; single-pass with sets expected.
  • LeetCode Discuss (2026-Q1)Listed in Vercel screen pool.

Problem

Determine if a 9x9 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 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition. Note: A Sudoku board (partially filled) could be valid but is not necessarily solvable.

Constraints

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

Examples

Example 1

Input
board = [['5','3','.','.','7','.','.','.','.'], ...]
Output
true

Example 2

Input
Same as above but board[0][0] = '8'
Output
false

Explanation: Two 8s in the top-left 3x3 box.

Approaches

1. Three nested passes

Validate rows, then columns, then 3x3 boxes — each in its own loop.

Time
O(3 * 81) = O(1)
Space
O(1)
// Three near-identical loops; verbose. The single-pass version is cleaner.

Tradeoff: Correct but repetitive.

2. Single-pass with three sets per index (optimal)

27 sets: 9 row-sets, 9 col-sets, 9 box-sets. For each filled cell, derive its row, column, and box (boxIdx = (r/3)*3 + c/3); check and insert into all three.

Time
O(1) (board size fixed)
Space
O(1)
function isValidSudoku(board) {
  const rows = Array.from({length: 9}, () => new Set());
  const cols = Array.from({length: 9}, () => new Set());
  const boxes = Array.from({length: 9}, () => 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 b = Math.floor(r / 3) * 3 + Math.floor(c / 3);
      if (rows[r].has(v) || cols[c].has(v) || boxes[b].has(v)) return false;
      rows[r].add(v);
      cols[c].add(v);
      boxes[b].add(v);
    }
  }
  return true;
}

Tradeoff: Single 9x9 pass with three constraint sets per cell. The box-index formula is the only piece that confuses candidates — write it out.

Vercel-specific tips

Vercel grades the single-pass approach with the box-index formula. Bonus signal: writing the formula `(r/3)*3 + c/3` and explaining that it groups every 3x3 block by its top-left corner. Mention that the alphabet is small (9 digits) so sets could be bit masks for a small speedup.

Common mistakes

  • Wrong box-index formula — most common bug. Test with r=4, c=5 (should be box 4, not 5 or 14).
  • Skipping the '.' check — empty cells must be ignored.
  • Reusing one set across iterations — corrupts the check.

Follow-up questions

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

  • Sudoku Solver (LC 37, hard) — backtracking using this validity check.
  • What if the board is NxN with sqrt(N)-sized boxes? Generalize the box formula.
  • Streaming Sudoku — cell filled one at a time, return validity each step.

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 does `(r/3)*3 + c/3` work for box index?

r/3 (integer divide) gives the box-row (0, 1, or 2). c/3 gives the box-column. Multiplying box-row by 3 and adding box-column gives 0..8 — a unique ID per 3x3 block.

Bit masks vs Sets?

Since digits are 1-9, a 9-bit mask per row/col/box works (set bit `1 << (digit - 1)`). Faster and zero allocation, but Sets are clearer for an interview.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →