Practice
longest after processing the input ["a", "app", "ap", "apply"]?Solution
Step 1: Insert words into trie
Words "a", "app", "ap", "apply" are inserted. Only nodes with complete words have word attribute set.Step 2: BFS traversal and update longest
Traversal visits "a" first, then "ap", then "app". "apply" is not valid because "appl" prefix is missing. The longest valid word found is "app".Final Answer:
Option B -> Option BQuick Check:
"app" is longest word with all prefixes present [OK]
- Assuming "apply" is valid without all prefixes
- Not updating longest correctly on equal length
- Skipping lex order in BFS children iteration
result after processing the entire searchWord?Solution
Step 1: Sort products lexicographically
Sorted products: ["mobile", "moneypot", "mouse"]Step 2: Build trie and collect suggestions
For prefix 'm': suggestions are ["mobile", "moneypot", "mouse"]. For prefix 'mo': same suggestions as all start with 'mo'.Final Answer:
Option B -> Option BQuick Check:
Result matches expected suggestions for prefixes 'm' and 'mo' [OK]
- Mixing order of suggestions
- Off-by-one in prefix traversal
- Appending suggestions after node moves
from typing import List
class TrieNode:
def __init__(self):
self.children = {}
self.word = None
class Solution:
def buildTrie(self, words):
root = TrieNode()
for w in words:
node = root
for c in w:
if c not in node.children:
node.children[c] = TrieNode()
node = node.children[c]
node.word = w
return root
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
root = self.buildTrie(words)
rows, cols = len(board), len(board[0])
result = []
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
board[r][c] = '#'
dfs(r+1, c, node)
dfs(r-1, c, node)
dfs(r, c+1, node)
dfs(r, c-1, node)
board[r][c] = char
for r in range(rows):
for c in range(cols):
dfs(r, c, root)
return result
Solution
Step 1: Trace DFS starting at 'o' (0,0)
"oath" is found by following o->a->t->h path on the board.Step 2: Trace DFS starting at 'e' (1,0)
"eat" is found by e->a->t path on the board.Final Answer:
Option D -> Option DQuick Check:
"pea" and "rain" cannot be formed on the board [OK]
- Including words not present on board
- Missing words found by DFS
- Not handling visited cells correctly
Solution
Step 1: Understand trie usage in counting pairs
The trie accumulates counts of inserted numbers to query how many have XOR less than limit with current number.Step 2: Identify the bug
The code does not reset or clear the trie between different calls or test cases, causing counts to accumulate incorrectly if reused, leading to wrong results.Final Answer:
Option B -> Option BQuick Check:
Not resetting trie causes stale counts to affect results [OK]
- Not resetting trie between test cases
- Incrementing count incorrectly
- Wrong bit traversal order
Solution
Step 1: Identify trie construction cost
Building the trie from n roots each of length l takes O(n * l) time.Step 2: Identify search cost per word
Searching each of the m words of length k in the trie takes O(k) time per word.Final Answer:
Option A -> Option AQuick Check:
Sum of build and search times matches O(n*l + m*k) [OK]
- Assuming O(m*n*l) like brute force
- Multiplying k and l unnecessarily
