Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with row or col instead of num.
Using assignment '=' instead of comparison '==='.
✗ Incorrect
We compare each cell in the row with the number we want to place. If any cell equals num, return true.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with row or col instead of num.
Using assignment '=' instead of comparison '==='.
✗ Incorrect
We check each cell in the column to see if it equals the number we want to place.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with row or col instead of num.
Using assignment '=' instead of comparison '==='.
✗ Incorrect
We check each cell in the 3x3 box to see if it equals the number we want to place.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping row and col arguments.
Passing num or board instead of row or col.
✗ Incorrect
We pass the current row and column to isInBox to check the 3x3 box correctly.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong function names in blanks.
Not resetting the cell to 0 after backtracking.
✗ Incorrect
We check if placing num is valid, then recursively try to solve the board. If it fails, reset cell to 0.