Sudoko Solver

Recursion FAQs (Hard) Hard

Create a program that fills in the blank cells in a Sudoku puzzle to solve it.


Every sudoku solution needs to follow to these guidelines:


1) In every row, the numbers 1 through 9 must appear exactly once.

2) In every column, the numbers 1 through 9 must appear exactly once.

3) In each of the grid's nine 3x3 sub-boxes, the numbers 1 through 9 must appear exactly once.


Empty cells are indicated by the '.' character.

Constraints

  • board.length = 9
  • board[i].length = 9
  • board[i][j] is a digit or '.'
  • It is guaranteed that input board has only one solution.

Hints

  • Use recursion to try filling in each empty cell. For each empty cell, try placing numbers 1 through 9. After placing a number, recursively attempt to solve the rest of the puzzle.
  • Maintain data structures (e.g., sets or arrays) to track used numbers for each row, column, and sub-grid. This avoids repeatedly iterating over the grid to check constraints. Terminate as soon as the puzzle is solved, as only one solution exists for a valid Sudoku.

Company Tags

Airbnb Visa McKinsey & Company ARM Stripe Bungie Alibaba Ernst & Young Flipkart Etsy Medtronic Zoho Zomato Broadcom Riot Games Goldman Sachs Uber Zynga Databricks Activision Blizzard Freshworks Optum Micron Technology Ubisoft Texas Instruments Google Microsoft Amazon Meta Apple Netflix Adobe