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 Java
for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { if (exist(board, word, i, j, 0)) { return [1]; } } } return false;
Attempts:
3 left
💡 Hint
Common Mistakes
Returning false inside the loop instead of true.
Returning 0 or null which are invalid for boolean return type.
✗ Incorrect
If the word is found starting from any cell, we return true to indicate success.
2fill in blank
mediumComplete the base case to check if the entire word has been matched.
DSA Java
if (index == word.length()) { return [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning false in the base case.
Returning the index or word length instead of a boolean.
✗ Incorrect
When index equals the word length, it means all characters matched, so return true.
3fill in blank
hardFix the error in boundary check to avoid ArrayIndexOutOfBoundsException.
DSA Java
if (row < 0 || row >= board.length || col < 0 || col >= [1] || board[row][col] != word.charAt(index)) { return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using board.length for columns which is the row count.
Using word length or index which are unrelated.
✗ Incorrect
The number of columns is board[0].length, so col must be less than that to be valid.
4fill in blank
hardFill both blanks to mark the cell as visited and then restore it after backtracking.
DSA Java
char temp = board[row][col]; board[row][col] = [1]; boolean found = exist(board, word, row + 1, col, index + 1) || exist(board, word, row - 1, col, index + 1) || exist(board, word, row, col + 1, index + 1) || exist(board, word, row, col - 1, index + 1); board[row][col] = [2]; return found;
Attempts:
3 left
💡 Hint
Common Mistakes
Not restoring the original character after recursion.
Using the wrong character to mark visited cells.
✗ Incorrect
We mark the cell with '#' to indicate visited, then restore original char stored in temp after recursion.
5fill in blank
hardFill all three blanks to complete the recursive calls for all four directions.
DSA Java
return exist(board, word, row + [1], col + [2], index + 1) || exist(board, word, row + [3], col, index + 1) || exist(board, word, row, col + 1, index + 1) || exist(board, word, row, col - 1, index + 1);
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong increments causing out of bounds or skipping cells.
Not covering all four directions.
✗ Incorrect
We move down (row+1,col), up (row-1,col), right (row,col+1), and left (row,col-1).
