Prepin
Log in

A

Sudoku Solution Finder

Approach / Explanation

This question requires you to develop an algorithm to solve a Sudoku puzzle. A typical Sudoku puzzle features a 9x9 grid filled with numbers, where each column, each row, and each of the nine 3x3 sub-grids within the grid must include all digits from 1 to 9. Some numbers in the grid are already provided and cannot be altered. The objective is to find a valid solution that fills every square with the appropriate digits. You are tasked with creating an algorithm that takes an incomplete Sudoku grid (which can be represented as a two-dimensional array) as input and produces a completed Sudoku grid as output. Your algorithm must effectively handle any valid incomplete Sudoku grid and generate a correct solution. To successfully tackle this question, it is essential to have a solid understanding of Sudoku rules and strong programming skills, particularly in managing matrices and implementing recursive algorithms.

Suggested Answer

Here is an efficient and straightforward approach to tackle the Sudoku Solver problem using a Backtracking Algorithm in Python: def solve_sudoku ( grid ) : if not find_empty_location ( grid ) : return True row , col = find_empty_location ( grid ) for digit in range ( 1 , 10 ) : if not used_in_row ( grid , row , digit ) and not used_in_col ( grid , col , digit ) and not used_in_box ( grid , row - row % 3 , col - col % 3 , digit ) : grid [ row ] [ col ] = digit if solve_sudoku ( grid ) : return True grid [ row ] [ col ] = 0 return False This algorithm begins by searching for an empty cell. Once located, it attempts to fill the cell with a number between 1 and 9 that has not yet been used in the corresponding row, column, or 3x3 grid. If no valid number can be found, the algorithm backtracks by resetting the current cell to zero and returning False. If all cells are filled successfully, it returns True, indicating a solution has been found. This code utilizes several helper functions ( find_empty_location() , used_in_row() , used_in_col() , used_in_box() ) to maintain the game state and enhance the clarity of the main solving algorithm.

Alternative Answer

Here is an alternative approach utilizing Depth-first search (DFS) in JavaScript: var solveSudoku = function ( board ) { dfs ( board ) ; } ; function dfs ( board ) { for ( let i = 0 ; i < 9 ; i ++ ) { for ( let j = 0 ; j < 9 ; j ++ ) { if ( board [ i ] [ j ] === '.' ) { for ( let num = 1 ; num <= 9 ; num ++ ) { if ( isValid ( board , i , j , String ( num ) ) ) { board [ i ] [ j ] = String ( num ) ; if ( dfs ( board ) ) { return true ; } else { board [ i ] [ j ] = '.' ; // undo move } } } return false ; // trigger backtracking } } } return true ; } function isValid ( board , row , col , num ) { for ( let i = 0 ; i < 9 ; i ++ ) { if ( board [ i ] [ col ] === num ) return false ; // check column if ( board [ row ] [ i ] === num ) return false ; // check row } let startRow = Math . floor ( row / 3 ) * 3 ; let startCol = Math . floor ( col / 3 ) * 3 ; for ( let i = 0 ; i < 3 ; i ++ ) { for ( let j = 0 ; j < 3 ; j ++ ) { if ( board [ i + startRow ] [ j + startCol ] === num ) return false ; // check 3*3 block } } return true ; } In this illustration, we recursively attempt to fill the grid from the top-left to the bottom-right with numbers ranging from 1 to 9. If a number does not violate the rules of Sudoku, we recursively proceed to the next number. When a number cannot be placed, we return false, backtrack to remove the last entry, and then try the following number. The isValid function is employed to verify if a number can occupy a specific position. Note: While both Python and JavaScript implementations utilize the Depth-First Search technique, they are executed in distinct programming languages, hence they are regarded as separate examples.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Apple

Product

AI Candidate AgentCompaniesBrowse JobsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin | All rights reserved.