Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if a number is already used in the given row.
DSA C
int usedInRow(int grid[9][9], int row, int num) { for (int col = 0; col < 9; col++) { if (grid[row][col] == [1]) { return 1; } } return 0; }
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 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 used in the given column.
DSA C
int usedInCol(int grid[9][9], int col, int num) { for (int row = 0; row < 9; row++) { if (grid[row][col] == [1]) { return 1; } } return 0; }
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 if 'num' is present in the given column by comparing each cell in that column with 'num'.
3fill in blank
hardFix the error in the code that checks if a number is used in the 3x3 box.
DSA C
int 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[row + boxStartRow][col + boxStartCol] == [1]) { return 1; } } } return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with boxStartRow or boxStartCol instead of 'num'.
Using assignment '=' instead of comparison '=='.
✗ Incorrect
We must check if the cell value equals 'num' to detect if 'num' is already in the 3x3 box.
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
int 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]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using row or col instead of num in usedInBox.
Checking if grid[row][col] equals num instead of 0.
✗ Incorrect
We check if 'num' is not in row, column, or box, and the cell is empty (0) before placing 'num'.
5fill in blank
hardFill all three blanks to complete the backtracking function that solves the Sudoku.
DSA C
int solveSudoku(int grid[9][9]) { int row, col; int found = 0; for (row = 0; row < 9; row++) { for (col = 0; col < 9; col++) { if (grid[row][col] == 0) { found = 1; break; } } if (found) break; } if (!found) return 1; for (int num = 1; num <= 9; num++) { if (isSafe(grid, row, col, [1])) { grid[row][col] = [2]; if (solveSudoku(grid)) return [3]; grid[row][col] = 0; } } return 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using row or col instead of num for placement.
Returning 0 instead of 1 on success.
✗ Incorrect
We try numbers 1 to 9 (num) in the empty cell. If safe, place num and recurse. If solved, return 1 (true).