Skip to main content

43. Valid Sudoku

mediumAsked at Workday

Determine if a 9x9 Sudoku board is valid. Workday uses this for multi-dimensional constraint checks — same shape as validating that role assignments don't conflict across departments, locations, and time bands.

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

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2025)Workday SDE2 phone screen.

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 digits 1-9 without repetition; each column similarly; each of the nine 3x3 sub-boxes similarly. A cell may be '.' indicating empty.

Constraints

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

Examples

Example 1

Input
Standard valid filled-in board
Output
true

Example 2

Input
Board with two 8s in first column
Output
false

Approaches

1. Three passes (rows, columns, boxes)

Loop three times, each time checking one dimension.

Time
O(81) = O(1)
Space
O(1)
// three separate 9x9 loops — works but redundant

Tradeoff: Works but reads the board three times.

2. Single pass with three sets per index

Track rows[i], cols[j], boxes[box]. For each cell, check + add to all three.

Time
O(81) = O(1)
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 i = 0; i < 9; i++) {
    for (let j = 0; j < 9; j++) {
      const v = board[i][j];
      if (v === '.') continue;
      const b = 3 * Math.floor(i / 3) + Math.floor(j / 3);
      if (rows[i].has(v) || cols[j].has(v) || boxes[b].has(v)) return false;
      rows[i].add(v); cols[j].add(v); boxes[b].add(v);
    }
  }
  return true;
}

Tradeoff: Single pass, three sets per dimension. The box index formula is the part candidates fumble.

Workday-specific tips

Workday grades the box index formula: 3 * floor(i/3) + floor(j/3). Walk through with i=4, j=7 (box 5) before coding. Bonus: mention how this generalizes to N^2 x N^2 boards.

Common mistakes

  • Wrong box index formula — most common bug.
  • Using a single set instead of one per row/col/box.
  • Adding the '.' to the set — wastes an entry.

Follow-up questions

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

  • Sudoku Solver (LC 37) — backtracking.
  • What if the board is N^2 x N^2?
  • Streaming variant: cells arrive in unknown order.

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

Boxes are indexed row-by-row in a 3x3 super-grid. floor(i/3) is the super-row (0-2). floor(j/3) is the super-column (0-2). Multiply super-row by 3 and add to get a unique index 0-8.

Bitmasks instead of sets?

Yes — 9-bit bitmask per row/col/box. Constant time AND/OR. Slightly faster but harder to read.

Practice these live with InterviewChamp.AI

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

Practice these live with InterviewChamp.AI →