Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the chessboard with empty cells represented by '.'
DSA Go
board := make([][]byte, n) for i := 0; i < n; i++ { board[i] = make([]byte, n) for j := 0; j < n; j++ { board[i][j] = [1] } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Q' instead of '.' which represents a queen, not an empty cell.
Using characters like 'X' or 'O' which are not standard for empty cells.
✗ Incorrect
The chessboard cells are initialized with '.' to represent empty spaces where queens can be placed later.
2fill in blank
mediumComplete the code to check if placing a queen at (row, col) is safe by checking the column above.
DSA Go
for i := 0; i < row; i++ { if board[i][[1]] == 'Q' { return false } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'row' instead of 'col' which checks the wrong column.
Using 'i' which is the row index, not the column.
✗ Incorrect
We check the same column 'col' in all rows above the current 'row' to ensure no queen is placed there.
3fill in blank
hardFix the error in the diagonal check to ensure no queen is placed on the upper left diagonal.
DSA Go
for i, j := row, col; i >= 0 && j >= 0; i, j = i-1, [1] { if board[i][j] == 'Q' { return false } }
Attempts:
3 left
💡 Hint
Common Mistakes
Incrementing j instead of decrementing causes checking the wrong diagonal.
Not changing j causes an infinite loop or wrong checks.
✗ Incorrect
To move diagonally up-left, both row and column indices decrease by 1 each step.
4fill in blank
hardFill both blanks to check the upper right diagonal for queens.
DSA Go
for i, j := row, col; i >= 0 && j < n; i, j = i-1, [1] { if board[i][j] == 'Q' { return false } } board[row][col] = [2]
Attempts:
3 left
💡 Hint
Common Mistakes
Using j-1 instead of j+1 for the diagonal movement.
Placing '.' instead of 'Q' when setting the queen.
✗ Incorrect
To check upper right diagonal, column index j increases by 1 while row decreases by 1. Then place a queen 'Q' at the position.
5fill in blank
hardFill all three blanks to complete the recursive backtracking function that tries to place queens row by row.
DSA Go
func solve(row int, board [][]byte, n int, solutions *[][]string) {
if row == n {
var copyBoard []string
for _, r := range board {
copyBoard = append(copyBoard, string(r))
}
*solutions = append(*solutions, copyBoard)
return
}
for col := 0; col < n; col++ {
if isSafe(board, row, col, n) {
board[row][col] = [1]
solve(row+[2], board, n, solutions)
board[row][col] = [3]
}
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Not backtracking by resetting the cell to '.' causes wrong solutions.
Incrementing row by wrong value or not incrementing at all.
✗ Incorrect
Place a queen 'Q' at the current cell, move to the next row by adding 1, and backtrack by resetting the cell to '.' after recursion.
