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 Java
for (int col = 0; col < 9; col++) { if (board[row][col] == [1]) { return false; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable instead of 'num'.
Checking columns instead of rows.
✗ Incorrect
We check if the number 'num' is already present in the row by comparing each cell in that row.
2fill in blank
mediumComplete the code to check if a number is already in the given column.
DSA Java
for (int row = 0; row < 9; row++) { if (board[row][col] == [1]) { return false; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable instead of 'num'.
Checking rows instead of columns.
✗ Incorrect
We check if 'num' is already present in the column by comparing each cell in that column.
3fill in blank
hardFix the error in calculating the starting row of the 3x3 box.
DSA Java
int boxStartRow = row - row % [1];Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 or 9 instead of 3.
Forgetting modulo operation.
✗ Incorrect
Each 3x3 box starts at a row index that is a multiple of 3, so we use modulo 3.
4fill in blank
hardFill both blanks to check if 'num' is in the 3x3 box starting at (boxStartRow, boxStartCol).
DSA Java
for (int r = [1]; r < [1] + 3; r++) { for (int c = [2]; c < [2] + 3; c++) { if (board[r][c] == num) { return false; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'row' or 'col' instead of box start variables.
Looping beyond the 3x3 box.
✗ Incorrect
We loop from the box start row and column to cover the 3x3 box cells.
5fill in blank
hardFill all three blanks to complete the backtracking solve method that tries numbers 1 to 9 in empty cells.
DSA Java
for (int num = [1]; num <= [2]; num++) { if (isSafe(board, row, col, num)) { board[row][col] = num; if (solveSudoku(board)) { return true; } else { board[row][col] = [3]; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Trying numbers outside 1-9 range.
Resetting cells to -1 instead of 0.
✗ Incorrect
We try numbers 1 to 9. If a number fits, we place it. If it leads to no solution, we reset the cell to 0 (empty).
