Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the chessboard as a 2D array filled with dots '.' representing empty cells.
DSA Javascript
const board = Array(n).fill().map(() => Array(n).fill([1]));Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Q' instead of '.' for empty cells.
Using 0 or null which are not characters.
✗ Incorrect
The board should be initialized with '.' to represent empty cells where queens can be placed later.
2fill in blank
mediumComplete the code to place a queen 'Q' on the board at row i and column j.
DSA Javascript
board[[1]][j] = 'Q';
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping row and column indices.
Using a fixed number instead of variable i.
✗ Incorrect
To place a queen at row i and column j, use board[i][j] = 'Q'.
3fill in blank
hardFix the error in the function that checks if placing a queen at (row, col) is safe by completing the diagonal check.
DSA Javascript
for (let 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 '.' instead of 'Q'.
Using 0 or null which are not valid board values.
✗ Incorrect
We check if there is a queen 'Q' on the diagonal; if yes, it's not safe.
4fill in blank
hardFill both blanks to complete the recursive function call and backtracking step in the N Queens solver.
DSA Javascript
if (solveNQueens([1] + 1)) return true; board[row][[2]] = '.'; // backtrack
Attempts:
3 left
💡 Hint
Common Mistakes
Incrementing column instead of row in recursion.
Removing queen from wrong position during backtracking.
✗ Incorrect
We move to the next row by calling solveNQueens(row + 1) and remove the queen from the current column during backtracking.
5fill in blank
hardFill all three blanks to complete the code that collects the current board configuration as a list of strings.
DSA Javascript
const result = []; for (let i = 0; i < n; i++) { result.push(board[i].[1]('')); } solutions.push(result.map(row => row.[2]())); // Clear the board for next solution for (let i = 0; i < n; i++) { board[i] = Array(n).fill([3]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using split instead of join.
Not copying the string before pushing.
Resetting board cells to 'Q' instead of '.'
✗ Incorrect
We join each row array into a string, copy it with slice(), and reset the board cells to '.' for the next solution.
