Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the chessboard with zeros.
DSA C++
int board[N][N] = {{0}};
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
board[i][j] = [1];
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 to initialize the board.
Using uninitialized values causing unpredictable behavior.
✗ Incorrect
The chessboard should be initialized with 0 to indicate empty cells.
2fill in blank
mediumComplete the code to check if a queen can be placed safely in the given row and column.
DSA C++
bool isSafe(int board[N][N], int row, int col) {
for (int i = 0; i < col; i++) {
if (board[row][i] == [1]) {
return false;
}
}
return true;
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 0 instead of 1.
Checking the wrong direction or column.
✗ Incorrect
We check if there is a queen (represented by 1) in the same row to the left.
3fill in blank
hardFix the error in the diagonal check for queen safety.
DSA C++
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) { if (board[i][j] == [1]) { return false; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 0 instead of 1 on the diagonal.
Incorrect loop conditions causing out-of-bounds errors.
✗ Incorrect
Queens are marked with 1, so we check if any queen is on the upper left diagonal.
4fill in blank
hardFill both blanks to place a queen and then remove it during backtracking.
DSA C++
board[row][col] = [1]; if (solveNQUtil(board, col + 1)) { return true; } board[row][col] = [2]; // backtrack
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 to place a queen.
Not resetting the cell to 0 during backtracking.
✗ Incorrect
Place queen with 1 and remove it by setting back to 0 during backtracking.
5fill in blank
hardFill all three blanks to complete the recursive function call and return statement.
DSA C++
bool solveNQUtil(int board[N][N], int col) {
if (col == [1]) {
return [2];
}
for (int i = 0; i < [3]; i++) {
if (isSafe(board, i, col)) {
board[i][col] = 1;
if (solveNQUtil(board, col + 1)) {
return true;
}
board[i][col] = 0;
}
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using false instead of true in base case return.
Incorrect loop range causing out-of-bounds errors.
✗ Incorrect
When column equals N, all queens are placed, so return true. Loop runs from 0 to N-1 rows.
