Reported August 2021
ZipRecruiterbacktracking

Solve 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 reportedly put a Sudoku solver in front of candidates in August 2021, and the first thing you notice is that brute force is dead on arrival. A 9 x 9 board with dozens of blanks and nine digits per blank is an astronomically large search space. This is backtracking with constraint tracking, not a clever formula. If you've seen it before, it's 25 lines. If you haven't, it's easy to botch the undo step under a clock. StealthCoder is the safety net if you blank during the live OA, but the pattern below is short enough to carry in your head.

The problem

Given a partially filled 9 x 9 Sudoku board, fill every empty cell so that the completed board is valid.
Each row, each column, and each of the nine 3 x 3 sub-boxes must contain the digits 1 through 9 exactly once. An empty cell contains '.'.
The given clues are valid, and the board has exactly one solution. Return the completed board.

Function
solveSudoku(board: char[][]) → char[][]

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 = [["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
The filled board contains every digit exactly once in each row, column, and sub-box.
Example 2
board = [[".","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","."]]
return = [["5","3","4","6","7","8","9","1","2"],["6","7","2","1","9","5","3","4","8"],["1","9","8","3","4","2","5","6","7"],["8","5","9","7","6","1","4","2","3"],["4","2","6","8","5","3","7","9","1"],["7","1","3","9","2","4","8","5","6"],["9","6","1","5","3","7","2","8","4"],["2","8","7","4","1","9","6","3","5"],["3","4","5","2","8","6","1","7","9"]]
The two missing corner values are forced by their rows and columns.

Constraints
board.length == 9.
board[i].length == 9.
Every cell is '.' or a digit from '1' through '9'.
The given clues obey the Sudoku rules.
The board has exactly one solution.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is pruning, not speed. Keep three sets of bitmasks or boolean arrays: one per row, one per column, one per 3 x 3 box (box index is (r/3)*3 + c/3). Collect all empty cells, then recurse cell by cell. For each cell, try digits 1 to 9 that aren't in its row, column, or box. Place it, update the sets, recurse, and if the recursion fails, undo everything and try the next digit. Return true only when every empty cell is filled. The common pitfalls are forgetting to restore state on backtrack, rescanning the whole board for validity at each step, and returning the wrong signal so the solver keeps going after a solution. A cheap upgrade is picking the empty cell with the fewest candidates first. Since the board has exactly one solution, you can stop at the first full fill. If your mind goes blank mid-OA, StealthCoder can supply this template invisibly.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Solve 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as sudoku solver. 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Solve a Sudoku Board FAQ

How hard is the ZipRecruiter Sudoku solver really?+

It's a known hard-tier problem, but the logic is mechanical. You aren't inventing an algorithm, you're writing clean backtracking with validity checks. Most failures come from messy undo logic or slow validation, not from misunderstanding the idea.

What's the trick to avoid timing out?+

Track used digits per row, column, and box in arrays or bitmasks so each placement check is constant time. Don't rescan the board. Precollect empty cells so you don't search for the next blank every call.

Do I need to pick the most constrained cell first?+

No, plain row-major order passes for typical 9 x 9 boards. Choosing the cell with the fewest candidates is a nice speedup if you have time, but get the simple version correct first.

How do I compute which 3 x 3 box a cell belongs to?+

Use (row / 3) * 3 + (col / 3) with integer division. That gives box indexes 0 through 8. Use that index into your box tracker so you never loop over the sub-box when checking a digit.

How do I prepare in 48 hours?+

Write the backtracking solver from scratch twice, with no peeking. Focus on the place, recurse, undo pattern and the return-true-on-success signal. Then test on the two blank-corner example to confirm forced cells fill correctly.

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