Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the search from the first cell in the grid.
DSA Go
for i := 0; i < len(board); i++ { for j := 0; j < len(board[0]); j++ { if board[i][j] == word[[1]] { if backtrack(board, word, i, j, 0) { return true } } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 as the starting index.
Using len(word) which is out of range.
✗ Incorrect
The search starts by matching the first character of the word, which is at index 0.
2fill in blank
mediumComplete the code to mark the current cell as visited during backtracking.
DSA Go
temp := board[i][j]
board[i][j] = [1]
// backtracking calls here
board[i][j] = tempAttempts:
3 left
💡 Hint
Common Mistakes
Using a space character which might be confusing.
Using '0' which could be a valid board character.
✗ Incorrect
Marking the cell with '#' indicates it is visited and prevents revisiting during the current path.
3fill in blank
hardFix the error in the recursive backtracking call to check all four directions.
DSA Go
directions := [][]int{{-1, 0}, {1, 0}, {0, -1}, {0, 1}}
for _, dir := range directions {
x, y := i+dir[0], j+dir[[1]]
if backtrack(board, word, x, y, index+1) {
return true
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 for both indices causing incorrect coordinates.
Using 2 or 3 which are out of range.
✗ Incorrect
The second element of dir is at index 1, representing the column offset.
4fill in blank
hardFill both blanks to check if the current position is valid inside the grid.
DSA Go
if i < 0 || i >= [1] || j < 0 || j >= [2] { return false }
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(word) which is unrelated to grid size.
Using len(board) - 1 without proper comparison.
✗ Incorrect
The row index i must be within 0 and len(board)-1, and column index j within 0 and len(board[0])-1.
5fill in blank
hardFill all three blanks to complete the backtracking function signature and base case.
DSA Go
func backtrack(board [][]byte, word string, i, j, [1] int) bool { if [2] == len(word) { return [3] } // rest of the code }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i' instead of 'index' for the position.
Returning false instead of true at base case.
✗ Incorrect
The function uses 'index' to track the current character position; when index equals word length, the word is found, so return true.
