Challenge - 5 Problems
N Queens Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1:30remaining
Output of N Queens Board Representation
What is the printed board state after placing 4 queens in the following positions on a 4x4 chessboard? Positions are given as (row, column): (0,1), (1,3), (2,0), (3,2). The board prints 'Q' for queen and '.' for empty cells.
DSA C
char board[4][4] = { {'.', 'Q', '.', '.'}, {'.', '.', '.', 'Q'}, {'Q', '.', '.', '.'}, {'.', '.', 'Q', '.'} }; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { printf("%c ", board[i][j]); } printf("\n"); }
Attempts:
2 left
💡 Hint
Visualize the board row by row, placing 'Q' at the given column index and '.' elsewhere.
✗ Incorrect
The board is a 4x4 grid. Each row has exactly one queen at the specified column. The rest are empty cells represented by '.'. The output prints each row with spaces between characters and a newline after each row.
🧠 Conceptual
intermediate1:00remaining
Number of Solutions for 4 Queens
How many distinct solutions exist for the classic 4 Queens problem on a 4x4 chessboard?
Attempts:
2 left
💡 Hint
Try to place queens row by row and count valid arrangements.
✗ Incorrect
The 4 Queens problem has exactly 2 distinct solutions where no two queens attack each other.
🔧 Debug
advanced2:00remaining
Identify the Bug in the Safety Check Function
Given the following C function to check if a queen can be safely placed at (row, col), which option correctly identifies the bug?
DSA C
int isSafe(int board[][N], int row, int col) { int i, j; for (i = 0; i < col; i++) if (board[row][i]) return 0; for (i = row, j = col; i >= 0 && j >= 0; i--, j--) if (board[i][j]) return 0; for (i = row, j = col; i < N && j >= 0; i++, j--) if (board[i][j]) return 0; return 1; }
Attempts:
2 left
💡 Hint
Check variable scope and parameters used inside the function.
✗ Incorrect
The function uses 'N' but it is not passed as a parameter or defined globally, causing a compilation error.
🚀 Application
advanced1:30remaining
Backtracking Steps Count for 4 Queens
How many times is the backtracking function called when solving the 4 Queens problem using the standard backtracking approach?
Attempts:
2 left
💡 Hint
Count all recursive calls including failed attempts.
✗ Incorrect
The standard backtracking algorithm for 4 Queens calls the recursive function 24 times in total.
❓ Predict Output
expert2:30remaining
Output of N Queens Solution Count for N=5
What is the output of the following C code that counts the number of solutions for the 5 Queens problem?
DSA C
#include <stdio.h> #define N 5 int count = 0; int isSafe(int board[][N], int row, int col) { int i, j; for (i = 0; i < col; i++) if (board[row][i]) return 0; for (i = row, j = col; i >= 0 && j >= 0; i--, j--) if (board[i][j]) return 0; for (i = row, j = col; i < N && j >= 0; i++, j--) if (board[i][j]) return 0; return 1; } void solveNQUtil(int board[][N], int col) { if (col == N) { count++; return; } for (int i = 0; i < N; i++) { if (isSafe(board, i, col)) { board[i][col] = 1; solveNQUtil(board, col + 1); board[i][col] = 0; } } } int main() { int board[N][N] = {0}; solveNQUtil(board, 0); printf("%d\n", count); return 0; }
Attempts:
2 left
💡 Hint
Recall the known number of solutions for 5 Queens problem.
✗ Incorrect
The 5 Queens problem has exactly 10 distinct solutions, so the code prints 10.