Challenge - 5 Problems
N Queens Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of N Queens Board Representation
What is the printed board state after placing queens for N=4 in the given Rust code?
DSA Rust
fn print_board(board: &Vec<Vec<char>>) {
for row in board {
for &cell in row {
print!("{} ", cell);
}
println!();
}
}
fn main() {
let board = vec![
vec!['.', 'Q', '.', '.'],
vec!['.', '.', '.', 'Q'],
vec!['Q', '.', '.', '.'],
vec!['.', '.', 'Q', '.'],
];
print_board(&board);
}Attempts:
2 left
💡 Hint
Look carefully at the positions of 'Q' in each row vector.
✗ Incorrect
The board is printed row by row, each cell separated by a space. The given board matches option A exactly.
🧠 Conceptual
intermediateNumber of Solutions for N=1 and N=2
How many distinct solutions exist for the N Queens problem when N=1 and N=2 respectively?
Attempts:
2 left
💡 Hint
Try placing queens on a 1x1 and 2x2 board and see if they can avoid attacking each other.
✗ Incorrect
For N=1, placing one queen on the single cell is a valid solution. For N=2, no arrangement exists where two queens do not attack each other.
🔧 Debug
advancedIdentify the Error in the Rust Backtracking Function
What error will this Rust function cause when solving N Queens for N=4?
fn is_safe(board: &Vec>, row: usize, col: usize) -> bool {
for i in 0..col {
if board[row][i] == 'Q' {
return false;
}
}
let mut i = row as i32;
let mut j = col as i32;
while i >= 0 && j >= 0 {
if board[i as usize][j as usize] == 'Q' {
return false;
}
i -= 1;
j -= 1;
}
let mut i2 = row as i32;
let mut j2 = col as i32;
while i2 < board.len() as i32 && j2 >= 0 {
if board[i2 as usize][j2 as usize] == 'Q' {
return false;
}
i2 += 1;
j2 -= 1;
}
true
}
Attempts:
2 left
💡 Hint
Look at how variables i and j are declared twice with let mut in the same scope.
✗ Incorrect
In Rust, redeclaring variables with let mut in the same scope causes a compilation error. The second let mut i and j redeclaration is invalid.
🚀 Application
advancedCount Solutions for N=5 Using Backtracking
What is the total number of distinct solutions for the N Queens problem when N=5?
Attempts:
2 left
💡 Hint
Try to recall or compute the number of solutions for N=5 from known results.
✗ Incorrect
The known number of distinct solutions for N=5 is 10.
🧠 Conceptual
expertTime Complexity of N Queens Backtracking Algorithm
What is the approximate time complexity of the backtracking algorithm for solving the N Queens problem?
Attempts:
2 left
💡 Hint
Consider how many ways queens can be placed row by row without conflicts.
✗ Incorrect
The backtracking algorithm tries to place queens row by row, pruning invalid positions. The worst-case complexity is roughly factorial in N, O(N!).
