Challenge - 5 Problems
Word Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Word Search Result for Simple Grid
What is the output of the following Java code that checks if the word "CAT" exists in the given grid using backtracking?
DSA Java
public class WordSearch { public static boolean exist(char[][] board, String word) { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { if (backtrack(board, word, i, j, 0)) { return true; } } } return false; } private static boolean backtrack(char[][] board, String word, int i, int j, int index) { if (index == word.length()) return true; if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || board[i][j] != word.charAt(index)) return false; char temp = board[i][j]; board[i][j] = '#'; boolean found = backtrack(board, word, i + 1, j, index + 1) || backtrack(board, word, i - 1, j, index + 1) || backtrack(board, word, i, j + 1, index + 1) || backtrack(board, word, i, j - 1, index + 1); board[i][j] = temp; return found; } public static void main(String[] args) { char[][] board = { {'C', 'A', 'T'}, {'X', 'Z', 'Y'}, {'B', 'O', 'T'} }; System.out.println(exist(board, "CAT")); } }
Attempts:
2 left
💡 Hint
Trace the search starting from each cell and check if the word "CAT" can be formed by moving horizontally or vertically.
✗ Incorrect
The word "CAT" can be found starting at board[0][0] moving right to board[0][1] and then board[0][2]. The backtracking returns true.
❓ Predict Output
intermediateOutput when Word is Not Present in Grid
What will be the output of the following Java code when searching for the word "DOG" in the given grid?
DSA Java
public class WordSearch { public static boolean exist(char[][] board, String word) { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { if (backtrack(board, word, i, j, 0)) { return true; } } } return false; } private static boolean backtrack(char[][] board, String word, int i, int j, int index) { if (index == word.length()) return true; if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || board[i][j] != word.charAt(index)) return false; char temp = board[i][j]; board[i][j] = '#'; boolean found = backtrack(board, word, i + 1, j, index + 1) || backtrack(board, word, i - 1, j, index + 1) || backtrack(board, word, i, j + 1, index + 1) || backtrack(board, word, i, j - 1, index + 1); board[i][j] = temp; return found; } public static void main(String[] args) { char[][] board = { {'C', 'A', 'T'}, {'X', 'Z', 'Y'}, {'B', 'O', 'T'} }; System.out.println(exist(board, "DOG")); } }
Attempts:
2 left
💡 Hint
Check if the letters D, O, G appear in sequence horizontally or vertically in the grid.
✗ Incorrect
The word "DOG" cannot be formed from adjacent letters in the grid, so the method returns false.
🧠 Conceptual
advancedUnderstanding Backtracking in Word Search
In the backtracking approach for word search, why do we temporarily mark the current cell as visited (e.g., with '#') before exploring neighbors?
Attempts:
2 left
💡 Hint
Think about what happens if the algorithm revisits the same cell while searching for the word.
✗ Incorrect
Marking the cell temporarily prevents revisiting it in the current path, avoiding cycles and incorrect matches. After exploring, the original letter is restored.
🔧 Debug
advancedIdentify the Bug in Word Search Backtracking Code
What is the bug in the following backtracking code snippet for word search that causes incorrect results?
DSA Java
private static boolean backtrack(char[][] board, String word, int i, int j, int index) { if (index == word.length()) return true; if (i < 0 || j < 0 || i >= board.length || j >= board[0].length) return false; if (board[i][j] != word.charAt(index)) return false; board[i][j] = '#'; boolean found = backtrack(board, word, i + 1, j, index + 1) || backtrack(board, word, i - 1, j, index + 1) || backtrack(board, word, i, j + 1, index + 1) || backtrack(board, word, i, j - 1, index + 1); return found; }
Attempts:
2 left
💡 Hint
Check what happens to the grid after marking a cell as '#' and before returning.
✗ Incorrect
The code marks the cell as '#' but never restores the original letter after exploring neighbors. This corrupts the grid and causes wrong results in other search paths.
🚀 Application
expertCount All Occurrences of a Word in Grid Using Backtracking
Given a grid and a word, which code snippet correctly counts all distinct occurrences of the word in the grid using backtracking (moving horizontally or vertically)?
Attempts:
2 left
💡 Hint
Counting all occurrences requires summing all successful paths, not just checking if one exists.
✗ Incorrect
Option C correctly sums all paths that form the word by returning 1 for each complete match and 0 otherwise, while restoring the grid state after each path.
