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 C++
bool usedInRow(int grid[9][9], int row, int num) { for (int col = 0; col < 9; col++) { if (grid[row][col] == [1]) { return true; } } return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable to compare, like 'row' or 'col' instead of 'num'.
Checking only the first column instead of all columns.
✗ Incorrect
We check if the number 'num' is present in the given row by comparing each cell in that row with 'num'.
2fill in blank
mediumComplete the code to check if a number is already in the given column.
DSA C++
bool usedInCol(int grid[9][9], int col, int num) { for (int row = 0; row < 9; row++) { if (grid[row][col] == [1]) { return true; } } return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable to compare, like 'row' or 'col' instead of 'num'.
Checking only the first row instead of all rows.
✗ Incorrect
We check if the number 'num' is present in the given column by comparing each cell in that column with 'num'.
3fill in blank
hardFix the error in checking if a number is already in the 3x3 box.
DSA C++
bool usedInBox(int grid[9][9], int boxStartRow, int boxStartCol, int num) { for (int row = 0; row < 3; row++) { for (int col = 0; col < 3; col++) { if (grid[boxStartRow + row][boxStartCol + col] == [1]) { return true; } } } return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with the wrong variable like 'row' or 'boxStartRow'.
Not adding the loop counters to the box start indices.
✗ Incorrect
We compare each cell in the 3x3 box with the number 'num' to check if it is already present.
4fill in blank
hardFill both blanks to complete the function that checks if it is safe to place a number in a cell.
DSA C++
bool isSafe(int grid[9][9], int row, int col, int num) { return !usedInRow(grid, row, num) && !usedInCol(grid, col, num) && !usedInBox(grid, row - row % 3, col - col % 3, [1]) && grid[row][col] == [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable for the number in the box check.
Checking if the cell equals 'row' or 'col' instead of 0.
✗ Incorrect
We check if 'num' is not in the row, column, or box, and also if the cell is empty (0) before placing the number.
5fill in blank
hardFill all three blanks to complete the backtracking function that solves the Sudoku.
DSA C++
bool solveSudoku(int grid[9][9]) { int row, col; bool emptyFound = false; for (row = 0; row < 9; row++) { for (col = 0; col < 9; col++) { if (grid[row][col] == 0) { emptyFound = true; break; } } if (emptyFound) break; } if (!emptyFound) return true; for (int num = 1; num <= 9; num++) { if (isSafe(grid, row, col, [1])) { grid[row][col] = [2]; if (solveSudoku(grid)) return true; grid[row][col] = [3]; } } return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variables for blanks, like 'row' instead of 'num'.
Not resetting the cell to 0 after backtracking.
✗ Incorrect
We try numbers 1 to 9 in the empty cell. If safe, place the number, then recursively solve. If it fails, reset to 0 (empty) and try next number.
