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 Rust
fn used_in_row(board: &[[u8; 9]; 9], row: usize, num: u8) -> bool { for col in 0..9 { if board[row][col] == [1] { return true; } } false }
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable like 'row' or 'col' instead of 'num'.
Comparing with 0 instead of the number.
✗ Incorrect
We compare each cell in the row with the number we want to place, so the correct variable is 'num'.
2fill in blank
mediumComplete the code to check if a number is already in the given column.
DSA Rust
fn used_in_col(board: &[[u8; 9]; 9], col: usize, num: u8) -> bool { for row in 0..9 { if board[row][col] == [1] { return true; } } false }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'row' or 'col' instead of 'num' in the comparison.
Comparing with a fixed number like 9.
✗ Incorrect
We compare each cell in the column with the number 'num' to check if it is already present.
3fill in blank
hardFix the error in the code to check if a number is in the 3x3 box starting at (box_start_row, box_start_col).
DSA Rust
fn used_in_box(board: &[[u8; 9]; 9], box_start_row: usize, box_start_col: usize, num: u8) -> bool { for row in box_start_row..box_start_row + 3 { for col in box_start_col..box_start_col + 3 { if board[row][col] == [1] { return true; } } } false }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with 'row' or 'col' instead of 'num'.
Using 'box_start_row' instead of 'num'.
✗ Incorrect
We want to check if the number 'num' is present in the 3x3 box, so we compare with 'num'.
4fill in blank
hardFill both blanks to complete the function that checks if it is safe to place a number at (row, col).
DSA Rust
fn is_safe(board: &[[u8; 9]; 9], row: usize, col: usize, num: u8) -> bool { !used_in_row(board, row, [1]) && !used_in_col(board, col, [2]) && !used_in_box(board, row - row % 3, col - col % 3, num) }
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'row' or 'col' instead of 'num' to the functions.
Using 0 instead of 'num'.
✗ Incorrect
We check if 'num' is not used in the row and column, so both blanks should be 'num'.
5fill in blank
hardFill all three blanks to complete the backtracking function that solves the Sudoku board.
DSA Rust
fn solve_sudoku(board: &mut [[u8; 9]; 9]) -> bool { for row in 0..9 { for col in 0..9 { if board[row][col] == 0 { for num in 1..=9 { if is_safe(board, row, col, [1]) { board[row][col] = [2]; if solve_sudoku(board) { return true; } board[row][col] = [3]; } } return false; } } } true }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variables instead of 'num' for placement.
Not resetting the cell to 0 during backtracking.
✗ Incorrect
We try placing 'num' in the cell, then backtrack by resetting to 0 if needed.
