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 a Simple Grid
What is the output of the following C# code that checks if the word "CAT" exists in the grid using backtracking?
DSA C#
using System; class Program { static char[][] board = new char[][] { new char[] {'C', 'A', 'T'}, new char[] {'X', 'Y', 'Z'}, new char[] {'A', 'B', 'C'} }; static bool Exist(char[][] board, string word) { int rows = board.Length; int cols = board[0].Length; bool[,] visited = new bool[rows, cols]; bool Backtrack(int r, int c, int index) { if (index == word.Length) return true; if (r < 0 || c < 0 || r >= rows || c >= cols) return false; if (visited[r, c] || board[r][c] != word[index]) return false; visited[r, c] = true; bool found = Backtrack(r + 1, c, index + 1) || Backtrack(r - 1, c, index + 1) || Backtrack(r, c + 1, index + 1) || Backtrack(r, c - 1, index + 1); visited[r, c] = false; return found; } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (Backtrack(i, j, 0)) return true; } } return false; } static void Main() { Console.WriteLine(Exist(board, "CAT")); } }
Attempts:
2 left
💡 Hint
Think about whether the word "CAT" can be found by moving up, down, left, or right in the grid.
✗ Incorrect
The word "CAT" is found in the first row of the grid from left to right, so the backtracking returns true.
❓ Predict Output
intermediateOutput of Word Search for Word Not Present
What is the output of the following C# code that checks if the word "DOG" exists in the grid using backtracking?
DSA C#
using System; class Program { static char[][] board = new char[][] { new char[] {'C', 'A', 'T'}, new char[] {'X', 'Y', 'Z'}, new char[] {'A', 'B', 'C'} }; static bool Exist(char[][] board, string word) { int rows = board.Length; int cols = board[0].Length; bool[,] visited = new bool[rows, cols]; bool Backtrack(int r, int c, int index) { if (index == word.Length) return true; if (r < 0 || c < 0 || r >= rows || c >= cols) return false; if (visited[r, c] || board[r][c] != word[index]) return false; visited[r, c] = true; bool found = Backtrack(r + 1, c, index + 1) || Backtrack(r - 1, c, index + 1) || Backtrack(r, c + 1, index + 1) || Backtrack(r, c - 1, index + 1); visited[r, c] = false; return found; } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (Backtrack(i, j, 0)) return true; } } return false; } static void Main() { Console.WriteLine(Exist(board, "DOG")); } }
Attempts:
2 left
💡 Hint
Check if the letters D, O, G appear connected in the grid.
✗ Incorrect
The word "DOG" is not present in the grid in any connected path, so the function returns false.
🧠 Conceptual
advancedUnderstanding Backtracking Visited State Reset
Why is it important to reset the visited state (visited[r, c] = false) after exploring all directions in the backtracking function?
Attempts:
2 left
💡 Hint
Think about how backtracking explores multiple paths and needs to reuse cells in different attempts.
✗ Incorrect
Resetting visited allows the algorithm to try other paths that may use the same cell later, enabling full exploration.
🔧 Debug
advancedIdentify the Bug in Word Search Backtracking Code
What error will the following code produce when searching for the word "CAT"?
DSA C#
using System; class Program { static char[][] board = new char[][] { new char[] {'C', 'A', 'T'}, new char[] {'X', 'Y', 'Z'}, new char[] {'A', 'B', 'C'} }; static bool Exist(char[][] board, string word) { int rows = board.Length; int cols = board[0].Length; bool[,] visited = new bool[rows, cols]; bool Backtrack(int r, int c, int index) { if (index == word.Length) return true; if (r < 0 || c < 0 || r >= rows || c >= cols) return false; if (visited[r, c] || board[r][c] != word[index]) return false; visited[r, c] = true; bool found = Backtrack(r + 1, c, index + 1) || Backtrack(r - 1, c, index + 1) || Backtrack(r, c + 1, index + 1) || Backtrack(r, c - 1, index + 1); // Missing visited reset here return found; } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (Backtrack(i, j, 0)) return true; } } return false; } static void Main() { Console.WriteLine(Exist(board, "CAT")); } }
Attempts:
2 left
💡 Hint
Check what happens if visited cells are never reset after recursion.
✗ Incorrect
Without resetting visited, cells remain marked visited, blocking other paths and causing false negatives.
🚀 Application
expertCount All Occurrences of a Word in Grid Using Backtracking
Given a grid and a word, how would you modify the backtracking algorithm to count all distinct occurrences of the word in the grid instead of just checking if it exists?
Attempts:
2 left
💡 Hint
Think about how to explore all possible paths and keep track of how many times the word appears.
✗ Incorrect
To count all occurrences, the algorithm must explore all paths and increment a counter each time the word is found.
