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 Go
func isInRow(board [][]int, row int, num int) bool {
for col := 0; col < 9; col++ {
if board[row][col] == [1] {
return true
}
}
return false
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the row index instead of the number to check.
Comparing with column index instead of the number.
✗ Incorrect
We check if the number 'num' is present in the given row by comparing each cell in that row to 'num'.
2fill in blank
mediumComplete the code to check if a number is already in the given column.
DSA Go
func isInCol(board [][]int, col int, num int) bool {
for row := 0; row < 9; row++ {
if board[row][col] == [1] {
return true
}
}
return false
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the column index instead of the number to check.
Comparing with row index instead of the number.
✗ Incorrect
We check if the number 'num' is present in the given column by comparing each cell in that column to 'num'.
3fill in blank
hardFix the error in the code to check if a number is in the 3x3 box.
DSA Go
func isInBox(board [][]int, startRow int, startCol int, num int) bool {
for row := 0; row < 3; row++ {
for col := 0; col < 3; col++ {
if board[startRow+row][startCol+col] == [1] {
return true
}
}
}
return false
}Attempts:
3 left
💡 Hint
Common Mistakes
Using startRow or startCol instead of the number to check.
Comparing with loop variables instead of the number.
✗ Incorrect
We check if the number 'num' is present in the 3x3 box by comparing each cell in that box to 'num'.
4fill in blank
hardFill both blanks to check if placing a number is valid in the Sudoku board.
DSA Go
func isValid(board [][]int, row int, col int, num int) bool {
boxStartRow := row - row % [1]
boxStartCol := col - col % [2]
return !isInRow(board, row, num) && !isInCol(board, col, num) && !isInBox(board, boxStartRow, boxStartCol, num)
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 9 or 1 instead of 3 for box size.
Using 0 which causes wrong box calculation.
✗ Incorrect
The Sudoku board is divided into 3x3 boxes, so we use 3 to find the starting row and column of the box.
5fill in blank
hardFill all three blanks to complete the backtracking solver function.
DSA Go
func solveSudoku(board [][]int) bool {
for row := 0; row < 9; row++ {
for col := 0; col < 9; col++ {
if board[row][col] == [1] {
for num := 1; num <= [2]; num++ {
if isValid(board, row, col, num) {
board[row][col] = num
if solveSudoku(board) {
return true
}
board[row][col] = [3]
}
}
return false
}
}
}
return true
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 for empty cells.
Trying numbers beyond 9.
Resetting cell to 1 instead of 0 after backtracking.
✗ Incorrect
Empty cells are marked with 0. We try numbers 1 to 9. If a number doesn't work, reset cell to 0.
