Skip to main content

43. Valid Sudoku

mediumAsked at Plaid

Determine if a 9x9 Sudoku board is valid (without solving it). Plaid asks this as a constraint-validation primitive — the same shape as their multi-constraint webhook payload validator.

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

Source citations

Public interview reports confirming this problem appears in Plaid loops.

  • Glassdoor (2025)Plaid platform-team screen.
  • LeetCode Discuss (2026)Plaid validation OA.

Problem

Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated 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 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
A standard valid Sudoku grid
Output
true

Example 2

Input
Grid with two '8's in the top-left 3x3 box
Output
false

Approaches

1. Three separate passes

Loop once for rows, once for columns, once for 3x3 boxes.

Time
O(81)
Space
O(9) per pass
// Three loops, each builds and checks Set per row/col/box.
// Works but iterates the board three times.

Tradeoff: Correct but a bit redundant. Mention as the naive starting point.

2. Single pass with three sets per index

Maintain rows[9], cols[9], boxes[9] sets. For each filled cell, check all three sets and add.

Time
O(81)
Space
O(81)
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 pass. The box index = (r/3)*3 + (c/3) maps 2D box coordinates to 1D — clean and avoids a 2D set.

Plaid-specific tips

Plaid grades this on the box-index calculation. Bonus signal: encode each (cell, value) as a single string key like 'row1:5', 'col2:5', 'box0:5' and use one Set — collapses three structures into one. Connect to multi-constraint validation where row=user-id, col=device, box=geo.

Common mistakes

  • Computing box index as r % 3 + c % 3 — wrong, gives only 5 distinct values.
  • Treating '.' as a digit and adding it to the set.
  • Forgetting that columns must also be checked — only doing rows.

Follow-up questions

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

  • Solve the Sudoku (LC 37) — full backtracking.
  • Validate partially-filled with multi-constraint approval rules.
  • Real-time validation as cells are filled.

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 floor(r/3)*3 + floor(c/3)?

Maps each (r,c) to a box index 0-8. The *3 spaces the box rows; the +floor(c/3) selects the box column.

Why three sets per index?

Each cell is constrained on three axes. Tracking each axis separately makes the validation O(1) per cell instead of O(81) per cell.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →