Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the search from the first cell.
DSA C++
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 (board[i][j] == word[0] && dfs(board, word, i, j, 0)) {
return [1];
}
}
}
return false;
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning false immediately instead of true when word is found.
Returning the character instead of a boolean.
✗ Incorrect
If the first character matches and dfs returns true, the word exists, so return true.
2fill in blank
mediumComplete the code to mark the current cell as visited during DFS.
DSA C++
bool dfs(vector<vector<char>>& board, string& word, int i, int j, int index) {
if (index == word.size()) return true;
if (i < 0 || i >= board.size() || j < 0 || j >= board[0].size() || board[i][j] != word[index]) return false;
char temp = board[i][j];
board[i][j] = [1];
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;
}Attempts:
3 left
💡 Hint
Common Mistakes
Not marking the cell as visited, causing infinite loops.
Using a character that might appear in the board.
✗ Incorrect
Marking the cell with '#' indicates it is visited to avoid revisiting during this path.
3fill in blank
hardFix the error in the base case that checks if the entire word is found.
DSA C++
bool dfs(vector<vector<char>>& board, string& word, int i, int j, int index) {
if (index [1] word.size()) return true;
// rest of code omitted for brevity
}Attempts:
3 left
💡 Hint
Common Mistakes
Using less than or greater than instead of equality.
Off-by-one errors in base case.
✗ Incorrect
The base case is when index equals the word length, meaning all characters matched.
4fill in blank
hardFill both blanks to complete the dictionary comprehension that maps words to their lengths if length > 3.
DSA C++
auto wordLengths = std::unordered_map<std::string, int>{};
for (const auto& word : words) {
if (word.size() [1] 3) {
wordLengths[word] = [2];
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using less than instead of greater than.
Using word.length() inconsistently.
✗ Incorrect
We check if word size is greater than 3, then store the size for that word.
5fill in blank
hardFill all three blanks to complete the recursive DFS call and backtracking steps.
DSA C++
bool dfs(vector<vector<char>>& board, string& word, int i, int j, int index) {
if (index == word.size()) return true;
if (i < 0 || i >= board.size() || j < 0 || j >= board[0].size() || board[i][j] != word[index]) return false;
char temp = board[i][j];
board[i][j] = [1];
bool found = dfs(board, word, i + [2], j, index + 1) ||
dfs(board, word, i - [2], j, index + 1) ||
dfs(board, word, i, j + [3], index + 1) ||
dfs(board, word, i, j - [3], index + 1);
board[i][j] = temp;
return found;
}Attempts:
3 left
💡 Hint
Common Mistakes
Not marking visited cell.
Using 0 instead of 1 for movement steps.
✗ Incorrect
Mark visited with '#', move one step in each direction (up/down/left/right) by adding or subtracting 1.
