Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the chessboard size variable.
DSA Java
int n = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Setting n to 0 or negative values which makes no sense for board size.
Using variable n itself instead of a number.
✗ Incorrect
The standard chessboard size for the N Queens problem is 8, so n should be set to 8.
2fill in blank
mediumComplete the code to initialize the chessboard as a 2D array.
DSA Java
int[][] board = new int[[1]][[1]];
Attempts:
3 left
💡 Hint
Common Mistakes
Using fixed number 8 instead of variable n.
Using zero or one which are invalid sizes.
✗ Incorrect
The board size should be n by n, so both dimensions use n.
3fill in blank
hardFix the error in the loop that tries to place queens in each row.
DSA Java
for (int row = 0; row [1] n; row++) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= causes ArrayIndexOutOfBoundsException.
Using > or >= causes the loop to never run.
✗ Incorrect
The loop should run while row is less than n, not less or equal, to avoid out of bounds.
4fill in blank
hardFill both blanks to check if a queen can be safely placed at board[row][col].
DSA Java
if (board[row][col] == [1] && isSafe(board, row, col) == [2]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for 1 instead of 0 means spot is occupied.
Checking isSafe == false means unsafe spot.
✗ Incorrect
A safe spot has no queen (0) and isSafe returns true if safe.
5fill in blank
hardFill all three blanks to place a queen, recurse, and backtrack if needed.
DSA Java
board[row][col] = [1]; if (solveNQueens(board, row + 1) == [2]) { return [3]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 to place queen means no queen placed.
Returning false on success breaks the recursion.
✗ Incorrect
Place queen with 1, if recursive call returns true, return true to continue solution.
