0
0
DSA Typescriptprogramming~10 mins

Sudoku Solver Using Backtracking in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if a number is already in the given row.

DSA Typescript
function isInRow(board: number[][], row: number, num: number): boolean {
  for (let col = 0; col < 9; col++) {
    if (board[row][col] === [1]) {
      return true;
    }
  }
  return false;
}
Drag options to blanks, or click blank then click option'
Acol
Brow
Cnum
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with row or col instead of num.
Using assignment '=' instead of comparison '==='.
2fill in blank
medium

Complete the code to check if a number is already in the given column.

DSA Typescript
function isInCol(board: number[][], col: number, num: number): boolean {
  for (let row = 0; row < 9; row++) {
    if (board[row][col] === [1]) {
      return true;
    }
  }
  return false;
}
Drag options to blanks, or click blank then click option'
Arow
Bcol
C0
Dnum
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with row or col instead of num.
Using assignment '=' instead of comparison '==='.
3fill in blank
hard

Fix the error in the code to check if a number is in the 3x3 box containing (row, col).

DSA Typescript
function isInBox(board: number[][], row: number, col: number, num: number): boolean {
  const boxStartRow = row - (row % 3);
  const boxStartCol = col - (col % 3);
  for (let r = boxStartRow; r < boxStartRow + 3; r++) {
    for (let c = boxStartCol; c < boxStartCol + 3; c++) {
      if (board[r][c] === [1]) {
        return true;
      }
    }
  }
  return false;
}
Drag options to blanks, or click blank then click option'
Anum
B0
Ccol
Drow
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with row or col instead of num.
Using assignment '=' instead of comparison '==='.
4fill in blank
hard

Fill both blanks to complete the function that checks if placing num at (row, col) is valid.

DSA Typescript
function isValid(board: number[][], row: number, col: number, num: number): boolean {
  return !isInRow(board, row, num) && !isInCol(board, col, num) && !isInBox(board, [1], [2], num);
}
Drag options to blanks, or click blank then click option'
Arow
Bcol
Cnum
Dboard
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping row and col arguments.
Passing num or board instead of row or col.
5fill in blank
hard

Fill all three blanks to complete the backtracking function that solves the Sudoku board.

DSA Typescript
function solveSudoku(board: number[][]): boolean {
  for (let row = 0; row < 9; row++) {
    for (let col = 0; col < 9; col++) {
      if (board[row][col] === 0) {
        for (let num = 1; num <= 9; num++) {
          if ([1](board, row, col, num)) {
            board[row][col] = num;
            if ([2](board)) {
              return true;
            }
            board[row][col] = [3];
          }
        }
        return false;
      }
    }
  }
  return true;
}
Drag options to blanks, or click blank then click option'
AisValid
BsolveSudoku
C0
DisInRow
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong function names in blanks.
Not resetting the cell to 0 after backtracking.