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 4 queens in the first valid solution of the 4-Queens problem?
DSA C#
static void PrintBoard(int[] positions) { for (int row = 0; row < positions.Length; row++) { for (int col = 0; col < positions.Length; col++) { if (positions[row] == col) Console.Write("Q "); else Console.Write(". "); } Console.WriteLine(); } } int[] solution = {1, 3, 0, 2}; PrintBoard(solution);
Attempts:
2 left
💡 Hint
Remember the array index represents the row, and the value at that index is the column where the queen is placed.
✗ Incorrect
The array {1, 3, 0, 2} means queens are placed at (row0,col1), (row1,col3), (row2,col0), and (row3,col2). Printing '.' for empty and 'Q' for queen gives option A.
🧠 Conceptual
intermediateNumber of Solutions for 1x1 and 2x2 Boards
How many valid solutions exist for the N Queens problem on a 1x1 board and a 2x2 board respectively?
Attempts:
2 left
💡 Hint
Think about how many queens can be placed without attacking each other on very small boards.
✗ Incorrect
On a 1x1 board, placing one queen is trivially valid (1 solution). On a 2x2 board, no arrangement exists where 2 queens don't attack each other (0 solutions).
🔧 Debug
advancedIdentify the Bug in the Diagonal Check
Given this snippet from an N Queens solver, which option correctly identifies the bug causing incorrect diagonal attack detection?
bool IsSafe(int row, int col, int[] positions) {
for (int i = 0; i < row; i++) {
if (positions[i] == col || Math.Abs(positions[i] - col) == Math.Abs(i - row)) {
return false;
}
}
return true;
}
Attempts:
2 left
💡 Hint
Check the logic of absolute differences for diagonal attacks carefully.
✗ Incorrect
The diagonal check uses absolute differences correctly: Math.Abs(positions[i] - col) == Math.Abs(i - row) detects diagonal attacks. The loop correctly checks all previous rows with i < row. The column check is also correct. So no bug here.
🚀 Application
advancedCount Solutions for 5-Queens Problem
What is the total number of distinct solutions for the 5-Queens problem?
Attempts:
2 left
💡 Hint
The 5-Queens problem has fewer solutions than 8-Queens but more than 4-Queens.
✗ Incorrect
The 5-Queens problem has exactly 10 distinct solutions. The options are designed so that 10 is the correct answer for unique solutions excluding symmetrical duplicates.
❓ Predict Output
expertOutput of Recursive Call Count in N Queens Solver
Consider this C# code snippet that counts how many times the recursive function is called when solving the 4-Queens problem. What is the printed output?
int count = 0;
void Solve(int row, int[] positions) {
count++;
if (row == positions.Length) return;
for (int col = 0; col < positions.Length; col++) {
bool safe = true;
for (int i = 0; i < row; i++) {
if (positions[i] == col || Math.Abs(positions[i] - col) == Math.Abs(i - row)) {
safe = false;
break;
}
}
if (safe) {
positions[row] = col;
Solve(row + 1, positions);
}
}
}
int[] pos = new int[4];
Solve(0, pos);
Console.WriteLine(count);
Attempts:
2 left
💡 Hint
Count includes all recursive calls including dead ends.
✗ Incorrect
The recursive function is called 24 times in total for the 4-Queens problem with this backtracking approach, counting all partial attempts including those that fail early.
