Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the chessboard with '.' representing empty cells.
DSA C#
char[][] board = new char[n][]; for (int i = 0; i < n; i++) { board[i] = new char[n]; for (int j = 0; j < n; j++) { board[i][j] = [1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Q' instead of '.' will mark all cells as queens initially.
Using 'X' or 'O' are not standard representations for empty cells.
✗ Incorrect
The board cells are initialized with '.' to represent empty spaces where queens can be placed.
2fill in blank
mediumComplete the code to check if a queen can be placed at board[row][col] safely.
DSA C#
for (int i = 0; i < row; i++) { if (board[i][[1]] == 'Q') return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'row' instead of 'col' will check the wrong column.
Using 'i' will check diagonal incorrectly here.
✗ Incorrect
We check the same column 'col' in all previous rows to see if a queen is already placed.
3fill in blank
hardFix the error in the diagonal check to ensure no queen attacks diagonally.
DSA C#
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) { if (board[i][[1]] == 'Q') return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i' instead of 'j' will check the wrong column index.
Using 'row' or 'col' here is incorrect as they are fixed indices.
✗ Incorrect
We check the upper-left diagonal by decreasing both row and column indices; 'j' is the column index here.
4fill in blank
hardFill both blanks to place a queen and then remove it during backtracking.
DSA C#
board[row][[1]] = 'Q'; if (SolveNQueens(board, row + 1)) return true; board[row][[2]] = '.';
Attempts:
3 left
💡 Hint
Common Mistakes
Using different indices for placing and removing causes incorrect backtracking.
Using 'row' instead of 'col' will cause index errors.
✗ Incorrect
We place and remove the queen at the same column 'col' in the current row.
5fill in blank
hardFill all three blanks to complete the recursive call and loop for placing queens.
DSA C#
for (int [1] = 0; [2] < board.Length; [3]++) { if (IsSafe(board, row, [2])) { board[row][[2]] = 'Q'; if (SolveNQueens(board, row + 1)) return true; board[row][[2]] = '.'; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop causes syntax errors.
Using 'row' instead of 'col' will cause logical errors.
✗ Incorrect
The loop variable is 'col' used consistently to try placing queens in each column.
