Challenge - 5 Problems
N Queens Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of placing queens on a 4x4 board
What is the printed board state after placing queens in the first valid solution for 4 queens?
DSA Javascript
function printBoard(board) {
return board.map(row => row.map(cell => cell ? 'Q' : '.').join(' ')).join('\n');
}
function solveNQueens(n) {
const board = Array.from({ length: n }, () => Array(n).fill(0));
const solutions = [];
function isSafe(row, col) {
for (let i = 0; i < row; i++) {
if (board[i][col]) return false;
}
for (let i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (board[i][j]) return false;
}
for (let i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {
if (board[i][j]) return false;
}
return true;
}
function backtrack(row) {
if (row === n) {
solutions.push(board.map(r => r.slice()));
return true;
}
for (let col = 0; col < n; col++) {
if (isSafe(row, col)) {
board[row][col] = 1;
if (backtrack(row + 1)) return true;
board[row][col] = 0;
}
}
return false;
}
backtrack(0);
return solutions[0];
}
const solution = solveNQueens(4);
console.log(printBoard(solution));Attempts:
2 left
💡 Hint
Think about the first valid arrangement of queens on a 4x4 board where no two queens attack each other.
✗ Incorrect
The first valid solution for 4 queens places queens at positions (0,1), (1,3), (2,0), and (3,2). This corresponds to option B.
🧠 Conceptual
intermediateNumber of solutions for 1 queen
How many distinct solutions exist for placing 1 queen on a 1x1 chessboard?
Attempts:
2 left
💡 Hint
With only one cell, placing one queen is straightforward.
✗ Incorrect
There is exactly one way to place one queen on a 1x1 board.
🔧 Debug
advancedIdentify the error in the N Queens safety check
What error does this code produce when checking if a queen can be placed safely?
function isSafe(board, row, col) {
for (let i = 0; i <= row; i++) {
if (board[i][col]) return false;
}
for (let i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (board[i][j]) return false;
}
for (let i = row - 1, j = col + 1; i >= 0 && j < board.length; i--, j++) {
if (board[i][j]) return false;
}
return true;
}
const board = [[0,0],[0,0]];
console.log(isSafe(board, 2, 1));
Attempts:
2 left
💡 Hint
Check the indices used when accessing the board array.
✗ Incorrect
The code tries to access board[2][1] but board has only 2 rows (indices 0 and 1), causing an undefined access and TypeError.
🚀 Application
advancedCount solutions for 5 queens
How many distinct solutions exist for placing 5 queens on a 5x5 chessboard so that no two queens attack each other?
Attempts:
2 left
💡 Hint
The number of solutions for 5 queens is a known classic result.
✗ Incorrect
There are exactly 10 distinct solutions for the 5-queens problem.
❓ Predict Output
expertOutput of partial board after placing 3 queens on 5x5
Given the following code placing queens on a 5x5 board, what is the printed board state after placing queens at (0,0), (1,2), and (2,4)?
Print 'Q' for queen and '.' for empty cell.
DSA Javascript
const board = Array.from({ length: 5 }, () => Array(5).fill('.')); board[0][0] = 'Q'; board[1][2] = 'Q'; board[2][4] = 'Q'; for (let row = 0; row < 5; row++) { console.log(board[row].join(' ')); }
Attempts:
2 left
💡 Hint
Check the exact positions where queens are placed.
✗ Incorrect
Queens are placed at (0,0), (1,2), and (2,4), so only those cells have 'Q'.
