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 code that checks if the word "CAT" exists in the grid?
DSA Python
def exist(board, word): rows, cols = len(board), len(board[0]) def backtrack(r, c, i): if i == len(word): return True if r < 0 or c < 0 or r >= rows or c >= cols or board[r][c] != word[i]: return False temp = board[r][c] board[r][c] = '#' found = (backtrack(r+1, c, i+1) or backtrack(r-1, c, i+1) or backtrack(r, c+1, i+1) or backtrack(r, c-1, i+1)) board[r][c] = temp return found for row in range(rows): for col in range(cols): if backtrack(row, col, 0): return True return False board = [ ['C','A','T'], ['X','Z','Y'], ['B','N','M'] ] print(exist(board, "CAT"))
Attempts:
2 left
💡 Hint
Check if the word "CAT" can be found by moving horizontally or vertically in the grid.
✗ Incorrect
The function returns True because the word "CAT" is found in the first row of the grid moving left to right.
❓ Predict Output
intermediateOutput of Word Search for Non-Existing Word
What will be the output of this code when searching for the word "DOG" in the given grid?
DSA Python
def exist(board, word): rows, cols = len(board), len(board[0]) def backtrack(r, c, i): if i == len(word): return True if r < 0 or c < 0 or r >= rows or c >= cols or board[r][c] != word[i]: return False temp = board[r][c] board[r][c] = '#' found = (backtrack(r+1, c, i+1) or backtrack(r-1, c, i+1) or backtrack(r, c+1, i+1) or backtrack(r, c-1, i+1)) board[r][c] = temp return found for row in range(rows): for col in range(cols): if backtrack(row, col, 0): return True return False board = [ ['D','O','G'], ['A','B','C'], ['X','Y','Z'] ] print(exist(board, "CAT"))
Attempts:
2 left
💡 Hint
The word "CAT" is not present in the grid.
✗ Incorrect
The function returns False because the word "CAT" cannot be found in the grid.
🔧 Debug
advancedIdentify the Error in Backtracking Code
What error will this code produce when trying to find the word "HELLO" in the grid?
DSA Python
def exist(board, word): rows, cols = len(board), len(board[0]) def backtrack(r, c, i): if i == len(word): return True if r < 0 or c < 0 or r >= rows or c >= cols or board[r][c] != word[i]: return False board[r][c] = '#' found = (backtrack(r+1, c, i+1) or backtrack(r-1, c, i+1) or backtrack(r, c+1, i+1) or backtrack(r, c-1, i+1)) board[r][c] = word[i] return found for row in range(rows): for col in range(cols): if backtrack(row, col, 0): return True return False board = [ ['H','E','L','L','O'], ['A','B','C','D','E'], ['F','G','H','I','J'] ] print(exist(board, "HELLO"))
Attempts:
2 left
💡 Hint
Consider the mutability of the board elements.
✗ Incorrect
The board is a list of lists of strings, which are immutable. Assigning board[r][c] = '#' causes a ValueError because strings are immutable in Python.
🚀 Application
advancedNumber of Words Found in Grid
Given a list of words and a grid, how many words can be found in the grid using the backtracking approach?
DSA Python
words = ["CAT", "DOG", "BIRD"] board = [ ['C','A','T'], ['D','O','G'], ['B','I','R'] ] # Assume exist function is defined as before count = 0 for w in words: if exist(board, w): count += 1 print(count)
Attempts:
2 left
💡 Hint
Check which words can be found by moving horizontally or vertically.
✗ Incorrect
"CAT" and "DOG" can be found in the grid, but "BIRD" cannot be found completely.
🧠 Conceptual
expertWhy Backtracking is Suitable for Word Search in Grid
Why is backtracking a good approach to solve the word search problem in a grid?
Attempts:
2 left
💡 Hint
Think about how backtracking explores paths and prunes wrong ones.
✗ Incorrect
Backtracking explores all possible paths for the word and abandons paths early if they don't match, making it efficient for this problem.
