Challenge - 5 Problems
Sudoku Solver Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Sudoku Board After One Step of Backtracking
Given the initial Sudoku board and one step of the backtracking solver, what is the state of the board after placing the first valid number in the first empty cell?
DSA Rust
let mut board = vec![
vec![5, 3, 0, 0, 7, 0, 0, 0, 0],
vec![6, 0, 0, 1, 9, 5, 0, 0, 0],
vec![0, 9, 8, 0, 0, 0, 0, 6, 0],
vec![8, 0, 0, 0, 6, 0, 0, 0, 3],
vec![4, 0, 0, 8, 0, 3, 0, 0, 1],
vec![7, 0, 0, 0, 2, 0, 0, 0, 6],
vec![0, 6, 0, 0, 0, 0, 2, 8, 0],
vec![0, 0, 0, 4, 1, 9, 0, 0, 5],
vec![0, 0, 0, 0, 8, 0, 0, 7, 9],
];
// After one step of backtracking, the first empty cell (0,2) is filled with 1Attempts:
2 left
💡 Hint
Look for the first empty cell and try numbers 1 to 9 to find the first valid placement.
✗ Incorrect
The first empty cell is at row 0, column 2. The number 1 is the first valid number that can be placed there without breaking Sudoku rules.
🧠 Conceptual
intermediateUnderstanding Backtracking in Sudoku Solver
Which of the following best describes the role of backtracking in solving Sudoku puzzles?
Attempts:
2 left
💡 Hint
Think about how the solver tries and undoes moves to find a valid solution.
✗ Incorrect
Backtracking is a method where the solver tries a number in an empty cell, checks if it fits, and if not, it reverts and tries another number, exploring all options systematically.
🔧 Debug
advancedIdentify the Error in Sudoku Validity Check Function
Consider this Rust function snippet that checks if placing a number in a cell is valid. What error will this code cause?
DSA Rust
fn is_valid(board: &Vec<Vec<u8>>, row: usize, col: usize, num: u8) -> bool { for i in 0..9 { if board[row][i] == num || board[i][col] == num { return false; } } let box_row = row / 3 * 3; let box_col = col / 3 * 3; for i in box_row..box_row + 3 { for j in box_col..box_col + 3 { if board[i][j] == num { return false; } } } true } // Usage example: is_valid(&board, 0, 0, 5);
Attempts:
2 left
💡 Hint
Check the ranges and indexing carefully.
✗ Incorrect
The function correctly checks the row, column, and 3x3 box for the number. The ranges are valid and indexing is safe for inputs 0 to 8.
❓ Predict Output
advancedFinal Solved Sudoku Board Output
What is the final solved Sudoku board after running a correct backtracking solver on this initial board?
DSA Rust
let board = vec![
vec![5, 3, 0, 0, 7, 0, 0, 0, 0],
vec![6, 0, 0, 1, 9, 5, 0, 0, 0],
vec![0, 9, 8, 0, 0, 0, 0, 6, 0],
vec![8, 0, 0, 0, 6, 0, 0, 0, 3],
vec![4, 0, 0, 8, 0, 3, 0, 0, 1],
vec![7, 0, 0, 0, 2, 0, 0, 0, 6],
vec![0, 6, 0, 0, 0, 0, 2, 8, 0],
vec![0, 0, 0, 4, 1, 9, 0, 0, 5],
vec![0, 0, 0, 0, 8, 0, 0, 7, 9],
];
// After solving, the board is:Attempts:
2 left
💡 Hint
Check carefully the known solved Sudoku solution for this puzzle.
✗ Incorrect
Option A matches the known correct solved Sudoku board for the given initial puzzle. Option A has a wrong number in the first row last column, B has misplaced numbers, and D has a zero in the last cell.
🚀 Application
expertTime Complexity of Sudoku Solver Using Backtracking
What is the worst-case time complexity of a backtracking Sudoku solver for a 9x9 board?
Attempts:
2 left
💡 Hint
Consider how many choices are tried for each empty cell in the worst case.
✗ Incorrect
In the worst case, the solver tries all 9 numbers for each of the 81 cells, leading to O(9^81) complexity. This is exponential and explains why backtracking can be slow for hard puzzles.
