Practice
x and a limit m. For each query, you must find the maximum XOR of x with any array element less than or equal to m. Which approach guarantees an optimal solution for large inputs?Solution
Step 1: Understand problem constraints
Brute force is too slow for large inputs; DP for subarrays doesn't apply since queries are arbitrary.Step 2: Recognize the pattern
Sorting nums and queries allows incremental trie insertion of eligible elements, enabling efficient max XOR queries.Final Answer:
Option A -> Option AQuick Check:
Sorting + trie insertion matches known optimal approach [OK]
- Thinking brute force is efficient enough
- Using DP for unrelated subproblems
- Greedy max element doesn't guarantee max XOR
def search(self, word: str) -> bool:
stack = [(0, self.root)]
while stack:
index, node = stack.pop()
if index == len(word):
if node.is_end:
return True
continue
ch = word[index]
if ch == '.':
for child in node.children:
stack.append((index + 1, child))
else:
if ch in node.children:
stack.append((index + 1, node.children[ch]))
return False
Solution
Step 1: Identify how children are iterated for '.' wildcard
The code uses 'for child in node.children:', which iterates over keys (characters), not child nodes.Step 2: Understand impact of iterating keys instead of nodes
Appending keys instead of TrieNode objects causes incorrect stack entries and search failure.Final Answer:
Option B -> Option BQuick Check:
Must iterate over node.children.values() to get child nodes [OK]
- Iterating keys instead of child nodes
- Forgetting to mark is_end
- Misplacing base case check
Solution
Step 1: Analyze trie construction
Inserting n words of length m takes O(n * m) time as each character is processed once.Step 2: Analyze BFS traversal
BFS visits each node once; each node has up to 26 children, so total BFS cost is O(n * 26) which is O(n).Final Answer:
Option A -> Option AQuick Check:
Trie build + BFS traversal sum to O(n*m + n*26) [OK]
- Confusing BFS cost as O(n*m*26)
- Forgetting BFS visits each node once
- Assuming sorting children adds log factor per node
def dfs(r, c, node):
if r < 0 or r >= rows or c < 0 or c >= cols:
return
char = board[r][c]
if char == '#' or char not in node.children:
return
node = node.children[char]
if node.word:
result.append(node.word)
node.word = None
# BUG: Missing marking visited cell
dfs(r+1, c, node)
dfs(r-1, c, node)
dfs(r, c+1, node)
dfs(r, c-1, node)
# BUG: Missing restoring cell after DFS
Solution
Step 1: Identify missing visited cell marking
The code does not mark the current cell as visited before recursive calls, allowing revisits and potential infinite loops.Step 2: Identify missing restoring cell state
Without restoring the cell after DFS, subsequent searches see corrupted board state, causing incorrect results.Final Answer:
Option B -> Option BQuick Check:
Missing visited marking causes infinite loops or incorrect word detection [OK]
- Forgetting to mark visited cells
- Not restoring board state after DFS
- Appending duplicates without clearing node.word
Solution
Step 1: Understand reuse allows cycles
Without visited marking, DFS can revisit the same cell infinitely, causing infinite recursion.Step 2: Add depth limit to DFS
Limiting recursion depth to the maximum word length prevents infinite loops while allowing reuse of cells.Final Answer:
Option A -> Option AQuick Check:
Depth limit bounds recursion, enabling correct search with reuse [OK]
- Removing visited marking without depth limit causes infinite loops
- Keeping visited marking contradicts reuse
- Switching to BFS doesn't solve infinite recursion
