Reported August 2021
ZipRecruiterhash table

Validate a Sudoku Board

Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live ZipRecruiter OA. Under 2s to a working solution.
Founder's read

ZipRecruiter reported this one in August 2021, and it's the classic Sudoku validity check. The board is fixed at 9 x 9, so there's no huge input to fear. The constraint that matters is the opposite: it's tiny, so the question is whether you can check rows, columns, and boxes cleanly in one pass without writing three messy loops. It's a hash-table problem wearing a grid costume. If you've got an OA invite and 48 hours, this is a pattern you can lock in tonight. StealthCoder is there as a safety net during the live OA if your mind goes blank on the box indexing.

The problem

Given a partially filled 9 x 9 Sudoku board, return true if every filled cell obeys the Sudoku rules and false otherwise.
Each row, each column, and each of the nine 3 x 3 sub-boxes may contain each digit from 1 through 9 at most once. An empty cell contains '.'.
Check only whether the filled cells are consistent. The board does not need to be solvable.

Function
isValidSudoku(board: char[][]) → boolean

Examples
Example 1
board = [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
return = true
No row, column, or sub-box repeats a filled digit.
Example 2
board = [["5","3",".",".","7",".",".",".","5"],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".",".",".","6","."],["8",".",".",".","6",".",".",".","3"],["4",".",".","8",".","3",".",".","1"],["7",".",".",".","2",".",".",".","6"],[".","6",".",".",".",".","2","8","."],[".",".",".","4","1","9",".",".","5"],[".",".",".",".","8",".",".","7","9"]]
return = false
The first row contains digit 5 twice.

Constraints
board.length == 9.
board[i].length == 9.
Every cell is '.' or a digit from '1' through '9'.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Brute force would compare every filled cell against its row, column, and box peers. On a 9 x 9 grid that's technically fine, but it's slow to write and easy to botch. The clean trick is one pass with sets. Keep nine sets for rows, nine for columns, nine for boxes. For each filled cell at (r, c), compute the box index as (r // 3) * 3 + c // 3. If the digit is already in any of the three sets, return false. Otherwise add it to all three. The common pitfall is the box index formula, people swap the terms or forget the multiply by 3. Another one is checking '.' cells. Skip them. Also remember you're not solving the board, only checking consistency. That's O(81), effectively constant. If you freeze on the formula during the live OA, StealthCoder can hand you the working version fast.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Validate a Sudoku Board cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as valid sudoku. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass ZipRecruiter's OA.

ZipRecruiter reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Validate a Sudoku Board FAQ

How hard is Validate a Sudoku Board really?+

It's easy to medium. There's no clever algorithm, just careful bookkeeping. The logic is simple, but off-by-one errors in the box index trip people up. If you can explain sets for rows, columns, and boxes, you're most of the way there.

What's the trick for the 3 x 3 sub-boxes?+

Map each cell to a box with (r // 3) * 3 + c // 3. That gives a number from 0 to 8. Use it to index your array of box sets. Test it on the corners: (0,0) is box 0, (8,8) is box 8.

Do I need to check if the board is solvable?+

No. The problem says to check only whether filled cells are consistent. Don't write a backtracking solver. Scan once, track seen digits, and return false on the first duplicate.

Can I do it with one pass?+

Yes. Loop through all 81 cells once. Skip '.', then check the digit against the row, column, and box sets. Return false on any hit, otherwise add it. Return true after the loop ends.

How do I prepare for this in 48 hours?+

Write it from scratch twice, once with sets and once with a seen-string like 'digit in row 3'. Then test Example 2 where row 0 repeats a 5. Also practice grid indexing, since the same skills show up in other matrix questions.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with ZipRecruiter.

OA at ZipRecruiter?
Invisible during screen share
Get it