Skip to main content

43. Valid Sudoku

mediumAsked at Salesforce

Validate a Sudoku board's rows, columns, and 3x3 sub-boxes. Salesforce uses this to test multi-dimensional validation in a single pass.

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

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce uses this to test multi-key set logic.
  • Blind (2025-11)Used as a brick-by-brick validation question.

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
board = [["5","3",".",".","7",".",".",".","."],...]
Output
true

Example 2

Input
board = [["8","3",".",".","7",".",".",".","."],...]
Output
false

Explanation: Two 8s in the first column.

Approaches

1. Three nested loops, one per constraint

Validate rows, columns, and boxes in three separate passes.

Time
O(3 * 81)
Space
O(81)
function isValidSudoku(board) {
  const valid = (cells) => new Set(cells).size === cells.length;
  for (let i = 0; i < 9; i++) {
    const row = board[i].filter(c => c !== '.');
    if (!valid(row)) return false;
    const col = board.map(r => r[i]).filter(c => c !== '.');
    if (!valid(col)) return false;
  }
  for (let bi = 0; bi < 3; bi++) for (let bj = 0; bj < 3; bj++) {
    const box = [];
    for (let i = 0; i < 3; i++) for (let j = 0; j < 3; j++) {
      const c = board[bi*3+i][bj*3+j];
      if (c !== '.') box.push(c);
    }
    if (!valid(box)) return false;
  }
  return true;
}

Tradeoff: Three passes but still O(81). Salesforce prefers the single-pass version.

2. Single pass with composite keys

One Set; for each non-empty cell, add three keys: 'row r v', 'col c v', 'box (r/3)(c/3) v'. Any duplicate insertion = invalid.

Time
O(81)
Space
O(81)
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 keys = [`r${r}-${v}`, `c${c}-${v}`, `b${Math.floor(r/3)}${Math.floor(c/3)}-${v}`];
    for (const k of keys) {
      if (seen.has(k)) return false;
      seen.add(k);
    }
  }
  return true;
}

Tradeoff: Single pass. The composite-key trick lets one Set track all three constraints simultaneously. Elegant — Salesforce favors this.

Salesforce-specific tips

Salesforce loves the composite-key approach because it generalizes to their multi-tenant data validation (org-id + field + value uniqueness). Bonus signal: explain that the prefix ('r', 'c', 'b') in the key keeps the three namespaces from colliding.

Common mistakes

  • Using only row index and value in the key — collides with column duplicates.
  • Forgetting to skip '.' cells — false positives.
  • Computing box index with Math.floor on each iteration without realizing it's box = (r/3, c/3).

Follow-up questions

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

  • Sudoku Solver (LC 37) — actually fill the board.
  • Generalize to NxN Sudoku.
  • Count the number of valid digits placeable in a given empty cell.

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 keys with 'r', 'c', 'b'?

To prevent namespace collisions. Without the prefix, 'r0-5' and 'c0-5' would collide, breaking the multi-constraint check.

Could I use three separate sets instead?

Yes, with rows[r], cols[c], boxes[b]. Slightly more code but equally efficient. The single-set composite-key version is more concise.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →