Skip to main content
All Questions

Sudoku Solver

Hard

Write a function sudokuSolve that checks whether a given sudoku board is solvable. If so, the function returns true. Otherwise (i.e. there is no valid solution to the given sudoku board), it returns false.

Context:

In sudoku, the objective is to fill a 9x9 board with digits so that each column, each row, and each of the nine 3x3 sub-boards that compose the board contains all of the digits from 1 to 9. The board setter provides a partially completed board, which for a well-posed board has a unique solution.

As explained above, for this problem, it suffices to calculate whether a given sudoku board has a solution. No need to return the actual numbers that make up a solution.

A sudoku board is represented as a two-dimensional 9x9 array of the characters '1', '2', ..., '9' and the '.' character, which represents a blank space. The function should fill the blank spaces with characters such that the following rules apply:

  1. In every row of the array, all characters '1', '2', ..., '9' appear exactly once.
  2. In every column of the array, all characters '1', '2', ..., '9' appear exactly once.
  3. In every 3x3 sub-board that is illustrated below, all characters '1', '2', ..., '9' appear exactly once.

A solved sudoku is a board with no blank spaces, i.e. all blank spaces are filled with characters that abide by the constraints above. If the function succeeds in solving the sudoku board, it'll return true (false, otherwise).

Example (more examples can be found here):

A typical Sudoku board

A typical Sudoku board setter

The same board with solution numbers marked in red

The same board with solution numbers marked in red

Be sure to write readable and efficient code, explain how your solution is built and why you chose to build it that way.

Related courses

Course

Machine Learning Engineer Interview Prep

Land your dream machine learning role at Meta, Google, Amazon, Apple, Microsoft, Nvidia, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Explore ML system design, core concepts, coding, behavioral interviews, and more.

Course

Data Science Interview Prep

Land your dream data science role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Practice statistics, experimentation, coding, SQL, machine learning, behavioral interviews, and more.