Challenge - 5 Problems
N Queens Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of N Queens solution count for N=4
What is the output of the following Java code that counts the number of solutions to the 4-Queens problem?
DSA Java
public class NQueens { private int count = 0; private boolean isSafe(int row, int col, int[] board) { for (int i = 0; i < row; i++) { if (board[i] == col || Math.abs(board[i] - col) == row - i) { return false; } } return true; } private void solve(int row, int n, int[] board) { if (row == n) { count++; return; } for (int col = 0; col < n; col++) { if (isSafe(row, col, board)) { board[row] = col; solve(row + 1, n, board); } } } public int totalNQueens(int n) { int[] board = new int[n]; solve(0, n, board); return count; } public static void main(String[] args) { NQueens nq = new NQueens(); System.out.println(nq.totalNQueens(4)); } }
Attempts:
2 left
💡 Hint
Recall the known number of solutions for 4-Queens problem.
✗ Incorrect
The 4-Queens problem has exactly 2 distinct solutions where 4 queens can be placed on a 4x4 board without threatening each other.
🧠 Conceptual
intermediateUnderstanding the safety check in N Queens
In the N Queens problem, what does the safety check condition Math.abs(board[i] - col) == row - i ensure?
Attempts:
2 left
💡 Hint
Think about how diagonals relate to row and column differences.
✗ Incorrect
Two queens are on the same diagonal if the difference in their columns equals the difference in their rows.
🔧 Debug
advancedIdentify the error in this N Queens solver snippet
What error will occur when running this code snippet for N Queens?
DSA Java
private void solve(int row, int n, int[] board) { if (row == n) { count++; return; } for (int col = 0; col <= n; col++) { if (isSafe(row, col, board)) { board[row] = col; solve(row + 1, n, board); } } }
Attempts:
2 left
💡 Hint
Check the loop boundary for columns.
✗ Incorrect
The loop runs with col <= n, but valid columns are 0 to n-1. Accessing board[row] = col is safe, but isSafe uses col which can be n, causing index errors.
🚀 Application
advancedNumber of solutions for 5 Queens problem
Using a correct N Queens solver, what is the number of distinct solutions for N=5?
Attempts:
2 left
💡 Hint
The 5-Queens problem has a known solution count.
✗ Incorrect
The 5-Queens problem has exactly 10 solutions, but only 8 are distinct when considering symmetry.
❓ Predict Output
expertOutput of printing one solution board for N=4
What is the printed output of the following Java code that prints one valid 4-Queens board solution?
DSA Java
public class NQueensPrint { private boolean isSafe(int row, int col, int[] board) { for (int i = 0; i < row; i++) { if (board[i] == col || Math.abs(board[i] - col) == row - i) { return false; } } return true; } private boolean solve(int row, int n, int[] board) { if (row == n) { return true; } for (int col = 0; col < n; col++) { if (isSafe(row, col, board)) { board[row] = col; if (solve(row + 1, n, board)) { return true; } } } return false; } public void printBoard(int[] board) { int n = board.length; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (board[i] == j) { System.out.print("Q "); } else { System.out.print(". "); } } System.out.println(); } } public static void main(String[] args) { NQueensPrint nq = new NQueensPrint(); int n = 4; int[] board = new int[n]; nq.solve(0, n, board); nq.printBoard(board); } }
Attempts:
2 left
💡 Hint
The solver returns the first valid solution it finds.
✗ Incorrect
The backtracking solver places queens row by row starting from column 0, so the first solution printed matches option C.
