Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the chessboard with empty cells represented by '.'
DSA Typescript
const board: string[][] = Array.from({ length: n }, () => Array(n).fill([1]));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Q' instead of '.' to initialize the board.
Using a space ' ' which can be confusing visually.
✗ Incorrect
The board should be initialized with '.' to represent empty cells.
2fill in blank
mediumComplete the code to check if placing a queen at (row, col) is safe by checking the same column.
DSA Typescript
for (let i = 0; i < row; i++) { if (board[i][[1]] === 'Q') return false; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'row' instead of 'col' which checks the wrong column.
Using 'i' which is the row index, not column.
✗ Incorrect
We check the same column 'col' in all previous rows to ensure no queen is placed there.
3fill in blank
hardFix the error in the diagonal check to correctly verify the upper left diagonal for a queen.
DSA Typescript
for (let i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) { if (board[i][[1]] === 'Q') return false; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i' as column index which is incorrect.
Using fixed 'col' or 'row' instead of loop variable 'j'.
✗ Incorrect
We must check the cell at (i, j) on the upper left diagonal, so use 'j' as the column index.
4fill in blank
hardFill both blanks to check the upper right diagonal for a queen safely.
DSA Typescript
for (let i = row - 1, j = col + [1]; i >= 0 && j < n; i--, j[2]) { if (board[i][j] === 'Q') return false; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-1' for initial j which moves left instead of right.
Using '--' which decreases j instead of increasing.
✗ Incorrect
We start at col + 1 and increment j each iteration to move upper right diagonal.
5fill in blank
hardFill all three blanks to place a queen, recurse, and then remove the queen (backtrack).
DSA Typescript
board[row][col] = [1]; if (solveNQueens(board, n, row + 1)) return true; board[row][col] = [2]; // backtrack return [3];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'true' instead of 'false' in the return statement causing incorrect recursion.
Not resetting the cell to '.' during backtracking.
✗ Incorrect
Place 'Q' to mark queen, backtrack by resetting to '.', and return false if no solution.