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
What is the output of the following C++ code that checks if the word "CAT" exists in the given grid using backtracking?
DSA C++
#include <iostream> #include <vector> #include <string> using namespace std; bool dfs(vector<vector<char>>& board, string& word, int i, int j, int index) { if (index == word.size()) return true; if (i < 0 || j < 0 || i >= board.size() || j >= board[0].size() || board[i][j] != word[index]) return false; char temp = board[i][j]; board[i][j] = '#'; bool found = dfs(board, word, i+1, j, index+1) || dfs(board, word, i-1, j, index+1) || dfs(board, word, i, j+1, index+1) || dfs(board, word, i, j-1, index+1); board[i][j] = temp; return found; } bool exist(vector<vector<char>>& board, string word) { for (int i = 0; i < board.size(); i++) { for (int j = 0; j < board[0].size(); j++) { if (dfs(board, word, i, j, 0)) return true; } } return false; } int main() { vector<vector<char>> board = { {'C', 'A', 'T'}, {'X', 'Z', 'T'}, {'Y', 'O', 'T'} }; string word = "CAT"; cout << (exist(board, word) ? "Found" : "Not Found") << endl; return 0; }
Attempts:
2 left
💡 Hint
Trace the DFS starting from each cell and check if the word "CAT" can be formed by moving up, down, left, or right.
✗ Incorrect
The word "CAT" can be found starting at board[0][0] = 'C', then board[0][1] = 'A', and board[0][2] = 'T'. The DFS returns true, so output is "Found".
❓ Predict Output
intermediateOutput of Word Search with No Match
What is the output of the following C++ code that checks if the word "DOG" exists in the given grid using backtracking?
DSA C++
#include <iostream> #include <vector> #include <string> using namespace std; bool dfs(vector<vector<char>>& board, string& word, int i, int j, int index) { if (index == word.size()) return true; if (i < 0 || j < 0 || i >= board.size() || j >= board[0].size() || board[i][j] != word[index]) return false; char temp = board[i][j]; board[i][j] = '#'; bool found = dfs(board, word, i+1, j, index+1) || dfs(board, word, i-1, j, index+1) || dfs(board, word, i, j+1, index+1) || dfs(board, word, i, j-1, index+1); board[i][j] = temp; return found; } bool exist(vector<vector<char>>& board, string word) { for (int i = 0; i < board.size(); i++) { for (int j = 0; j < board[0].size(); j++) { if (dfs(board, word, i, j, 0)) return true; } } return false; } int main() { vector<vector<char>> board = { {'C', 'A', 'T'}, {'X', 'Z', 'T'}, {'Y', 'O', 'T'} }; string word = "DOG"; cout << (exist(board, word) ? "Found" : "Not Found") << endl; return 0; }
Attempts:
2 left
💡 Hint
Check if the letters 'D', 'O', 'G' appear in sequence in the grid.
✗ Incorrect
The word "DOG" cannot be formed from the given grid by moving up, down, left, or right. So the output is "Not Found".
🔧 Debug
advancedIdentify the Bug in Word Search Backtracking
The following code is intended to find if a word exists in a grid using backtracking. However, it sometimes returns incorrect results. What is the bug?
DSA C++
#include <vector> #include <string> using namespace std; bool dfs(vector<vector<char>>& board, string& word, int i, int j, int index) { if (index == word.size()) return true; if (i < 0 || j < 0 || i >= board.size() || j >= board[0].size() || board[i][j] != word[index]) return false; board[i][j] = '#'; bool found = dfs(board, word, i+1, j, index+1) || dfs(board, word, i-1, j, index+1) || dfs(board, word, i, j+1, index+1) || dfs(board, word, i, j-1, index+1); return found; } bool exist(vector<vector<char>>& board, string word) { for (int i = 0; i < board.size(); i++) { for (int j = 0; j < board[0].size(); j++) { if (dfs(board, word, i, j, 0)) return true; } } return false; }
Attempts:
2 left
💡 Hint
Think about what happens to the board cells after marking them visited.
✗ Incorrect
The code marks a cell as '#' to indicate visited but does not restore it after DFS calls. This corrupts the board for other paths, causing incorrect results.
❓ Predict Output
advancedOutput of Word Search with Repeated Letters
What is the output of the following C++ code that checks if the word "TOT" exists in the given grid using backtracking?
DSA C++
#include <iostream> #include <vector> #include <string> using namespace std; bool dfs(vector<vector<char>>& board, string& word, int i, int j, int index) { if (index == word.size()) return true; if (i < 0 || j < 0 || i >= board.size() || j >= board[0].size() || board[i][j] != word[index]) return false; char temp = board[i][j]; board[i][j] = '#'; bool found = dfs(board, word, i+1, j, index+1) || dfs(board, word, i-1, j, index+1) || dfs(board, word, i, j+1, index+1) || dfs(board, word, i, j-1, index+1); board[i][j] = temp; return found; } bool exist(vector<vector<char>>& board, string word) { for (int i = 0; i < board.size(); i++) { for (int j = 0; j < board[0].size(); j++) { if (dfs(board, word, i, j, 0)) return true; } } return false; } int main() { vector<vector<char>> board = { {'T', 'O', 'T'}, {'O', 'T', 'O'}, {'T', 'O', 'T'} }; string word = "TOT"; cout << (exist(board, word) ? "Found" : "Not Found") << endl; return 0; }
Attempts:
2 left
💡 Hint
Look for the word "TOT" starting from multiple positions and consider backtracking.
✗ Incorrect
The word "TOT" can be found in multiple paths in the grid. The DFS finds at least one valid path, so output is "Found".
🧠 Conceptual
expertTime Complexity of Word Search Backtracking
What is the worst-case time complexity of the backtracking algorithm used to find a word of length k in an m x n grid?
Attempts:
2 left
💡 Hint
Consider that from each cell, you explore up to 4 directions for each character in the word.
✗ Incorrect
At each of the k characters, the algorithm explores up to 4 directions from each cell. Since there are m*n starting cells, worst-case complexity is O(m * n * 4^k).
