0
0
DSA Typescriptprogramming~10 mins

N Queens Problem in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A'.'
B'x'
C'Q'
D' '
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Q' instead of '.' to initialize the board.
Using a space ' ' which can be confusing visually.
2fill in blank
medium

Complete 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'
Ai
B0
Ccol
Drow
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.
3fill in blank
hard

Fix 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'
Acol
Bi
Crow
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i' as column index which is incorrect.
Using fixed 'col' or 'row' instead of loop variable 'j'.
4fill in blank
hard

Fill 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'
A1
B-1
C+= 1
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-1' for initial j which moves left instead of right.
Using '--' which decreases j instead of increasing.
5fill in blank
hard

Fill 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'
A'Q'
B'.'
Cfalse
Dtrue
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.