Practice
Solution
Step 1: Trace insert of "apple" with value 3
Delta = 3 - 0 = 3. Root.sum += 3 -> 3. For each character 'a','p','p','l','e', node.sum += 3. After first insert, node at second 'p' has sum 3.Step 2: Trace insert of "app" with value 2
Delta = 2 - 0 = 2. Root.sum += 2 -> 5. For 'a','p','p', node.sum += 2. Node at second 'p' sum was 3, now 3 + 2 = 5.Final Answer:
Option B -> Option BQuick Check:
Sum at second 'p' after both inserts is 5 [OK]
- Forgetting to add delta on second insert
- Confusing first and second 'p' nodes
- Using old value instead of delta
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
Solution
Step 1: Identify empty trie scenario
If no nums ≤ m, trie is empty; node.children[bit] can be None.Step 2: Check max_xor traversal
Line assigning node = node.children[bit] assumes child exists, causing runtime error or wrong result.Final Answer:
Option A -> Option AQuick Check:
Must handle empty child case to avoid errors [OK]
- Ignoring empty trie case
- Assuming children always exist
- Not returning -1 when no valid nums
Solution
Step 1: Identify empty string edge case
Empty string pairs with palindromes require special handling to add pairs involving empty string indices.Step 2: Check code for empty string handling
Neither add_word nor search explicitly handle empty string cases, causing missing pairs.Final Answer:
Option A -> Option AQuick Check:
Empty string handling is a known subtle bug [OK]
- Assuming palindrome checks cover empty strings
- Missing empty string special case
- Confusing index assignments
Solution
Step 1: Identify missing sorting
The code does not sort products before building the trie, so suggestions are appended in input order, not lex order.Step 2: Consequences of missing sort
Without sorting, the suggestions list at each node is not guaranteed to be lexicographically smallest three, violating problem requirements.Final Answer:
Option A -> Option AQuick Check:
Sorting before trie build is essential for correct lex order [OK]
- Forgetting to sort products
- Appending suggestions without limit
- Not handling missing prefix nodes
