Bird
Raised Fist0
DSA Typescriptprogramming~30 mins

Sudoku Solver Using Backtracking in DSA Typescript - Build from Scratch

Choose your learning style10 modes available
Sudoku Solver Using Backtracking
📋 What You'll Learn
💡 Why This Matters
🌍 Real World
Sudoku solvers are examples of constraint satisfaction problems used in AI and logic programming. Understanding backtracking helps solve puzzles and complex decision problems.
💼 Career
Backtracking algorithms are used in software engineering for solving puzzles, scheduling, and optimization problems. This project builds problem-solving and algorithmic thinking skills valuable for coding interviews and real-world tasks.
Progress0 / 4 steps
1
Setup the Sudoku board
Create a 9x9 two-dimensional array called board with the exact values given below. Use 0 to represent empty cells.

Row 0: [5, 3, 0, 0, 7, 0, 0, 0, 0]
Row 1: [6, 0, 0, 1, 9, 5, 0, 0, 0]
Row 2: [0, 9, 8, 0, 0, 0, 0, 6, 0]
Row 3: [8, 0, 0, 0, 6, 0, 0, 0, 3]
Row 4: [4, 0, 0, 8, 0, 3, 0, 0, 1]
Row 5: [7, 0, 0, 0, 2, 0, 0, 0, 6]
Row 6: [0, 6, 0, 0, 0, 0, 2, 8, 0]
Row 7: [0, 0, 0, 4, 1, 9, 0, 0, 5]
Row 8: [0, 0, 0, 0, 8, 0, 0, 7, 9]
DSA Typescript
Hint

Use a constant named board and assign it a 2D array with the exact rows and values given.

2
Create the isSafe helper function
Create a function called isSafe that takes board, row, col, and num as parameters and returns true if placing num in board[row][col] does not break Sudoku rules. Check the row, column, and 3x3 box for duplicates.
DSA Typescript
Hint

Check the row and column for the number, then check the 3x3 box using the starting row and column calculated by row - (row % 3) and col - (col % 3).

3
Create the solveSudoku function using backtracking
Create a function called solveSudoku that takes board as a parameter and returns true if the Sudoku is solved. Use nested loops to find an empty cell (value 0). For each empty cell, try numbers 1 to 9. Use isSafe to check if the number can be placed. If yes, place the number and recursively call solveSudoku. If recursion returns false, reset the cell to 0 and try the next number. Return true if the board is solved, otherwise false.
DSA Typescript
Hint

Use nested loops to find empty cells. Try numbers 1 to 9. Use recursion to continue solving. Backtrack by resetting cells if needed.

4
Print the solved Sudoku board
Call solveSudoku(board) to solve the puzzle. Then print the solved board row by row. Use console.log to print each row as a space-separated string of numbers.
DSA Typescript
Hint

Call solveSudoku(board). If it returns true, print each row of board using console.log(board[row].join(' ')).

Practice

(1/5)
1. What is the main idea behind using backtracking to solve a Sudoku puzzle?
easy
A. Use a sorting algorithm to arrange numbers in each row
B. Try placing numbers one by one and backtrack if a conflict occurs
C. Fill all empty cells randomly and check if the puzzle is solved
D. Replace all zeros with the number 9 directly

Solution

  1. Step 1: Understand backtracking approach

    Backtracking tries numbers in empty cells one by one and checks if they fit the Sudoku rules.
  2. Step 2: Handle conflicts by undoing choices

    If a number causes a conflict later, backtracking removes it and tries the next number.
  3. Final Answer:

    Try placing numbers one by one and backtrack if a conflict occurs -> Option B
  4. Quick Check:

    Backtracking = Try and undo choices [OK]
Hint: Backtracking means try, check, undo if needed [OK]
Common Mistakes:
  • Thinking backtracking fills cells randomly
  • Assuming sorting solves Sudoku
  • Replacing zeros without checks
2. Which of the following TypeScript function signatures correctly represents a function to check if placing a number is valid in Sudoku?
easy
A. function isValid(board: number[][], row: number, col: number, num: number): boolean
B. function isValid(board: number[], row: number, col: number): number
C. function isValid(board: string[][], num: string): void
D. function isValid(board: number[][], num: number): string

Solution

  1. Step 1: Analyze function parameters

    The function needs the board (2D array), row, column, and number to check validity.
  2. Step 2: Check return type

    It should return a boolean indicating if placing the number is valid or not.
  3. Final Answer:

    function isValid(board: number[][], row: number, col: number, num: number): boolean -> Option A
  4. Quick Check:

    Correct parameters and boolean return [OK]
Hint: Check function inputs and boolean output for validity [OK]
Common Mistakes:
  • Using wrong parameter types
  • Missing row or column parameters
  • Returning wrong data type
3. Given the following partial Sudoku board and the backtracking function call solveSudoku(board), what will be the value at board[0][2] after the first successful placement?
const board = [
  [5, 3, 0, 0, 7, 0, 0, 0, 0],
  [6, 0, 0, 1, 9, 5, 0, 0, 0],
  [0, 9, 8, 0, 0, 0, 0, 6, 0],
  [8, 0, 0, 0, 6, 0, 0, 0, 3],
  [4, 0, 0, 8, 0, 3, 0, 0, 1],
  [7, 0, 0, 0, 2, 0, 0, 0, 6],
  [0, 6, 0, 0, 0, 0, 2, 8, 0],
  [0, 0, 0, 4, 1, 9, 0, 0, 5],
  [0, 0, 0, 0, 8, 0, 0, 7, 9]
];
medium
A. 4
B. 2
C. 1
D. 7

Solution

  1. Step 1: Identify empty cell and possible numbers

    At position (0,2), possible numbers are those not in row 0, column 2, or 3x3 box. Candidates: 1, 2, 4.
  2. Step 2: Check numbers 1 to 9 for validity

    1, 2, 4 are initially valid. Backtracking tries 1 first, recurses but backtracks due to later conflicts; tries 2, same; 4 succeeds and solves the puzzle.
  3. Final Answer:

    4 -> Option A
  4. Quick Check:

    First successful number placed = 4 [OK]
Hint: Check row, column, box for smallest valid number [OK]
Common Mistakes:
  • Choosing a number already in the same column
  • Ignoring 3x3 box constraints
  • Assuming first empty cell is filled with 1 always
4. Identify the bug in this TypeScript snippet for checking if a number can be placed in a Sudoku cell:
function isValid(board: number[][], row: number, col: number, num: number): boolean {
  for (let i = 0; i < 9; i++) {
    if (board[row][i] === num) return false;
    if (board[i][col] === num) return false;
  }
  const startRow = row - row % 3;
  const startCol = col - col % 3;
  for (let i = startRow; i < startRow + 3; i++) {
    for (let j = startCol; j < startCol + 3; j++) {
      if (board[i][j] === num) return false;
    }
  }
  return true;
}
medium
A. The function does not check the entire row correctly
B. The calculation of startRow and startCol is incorrect
C. The function checks the row and column twice causing errors
D. No bug; the function correctly checks validity

Solution

  1. Step 1: Review row and column checks

    The loop checks all cells in the given row and column for the number.
  2. Step 2: Review 3x3 box check

    startRow and startCol correctly find the top-left cell of the 3x3 box; nested loops check all 9 cells.
  3. Final Answer:

    No bug; the function correctly checks validity -> Option D
  4. Quick Check:

    All constraints checked properly [OK]
Hint: Check row, column, and box loops carefully [OK]
Common Mistakes:
  • Miscomputing startRow or startCol
  • Forgetting to check 3x3 box
  • Returning false too early
5. You want to optimize the Sudoku solver by stopping early when the puzzle is solved. Which modification to the backtracking function is best to achieve this?
hard
A. Only check rows and columns, skip 3x3 box checks
B. Continue trying all numbers in all cells even after solution found
C. Return true immediately when a valid number is placed in the last empty cell
D. Remove the base case and let recursion run fully

Solution

  1. Step 1: Understand base case in backtracking

    The base case returns true when all cells are filled correctly, stopping recursion.
  2. Step 2: Modify to return true immediately on last cell

    Returning true immediately when the last empty cell is filled avoids unnecessary further checks.
  3. Final Answer:

    Return true immediately when a valid number is placed in the last empty cell -> Option C
  4. Quick Check:

    Stop recursion early by returning true [OK]
Hint: Return true at last cell to stop recursion early [OK]
Common Mistakes:
  • Ignoring base case and continuing recursion
  • Skipping box checks causing wrong solutions
  • Trying all numbers even after solution
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard