Practice
1. Given the following Trie implementation, after inserting the words ['cat', 'car', 'dog'], what is the value of
node.isEnd after processing the last character of 'car' in the insert method?easy
Solution
Step 1: Trace insertion of 'car'
For each character 'c', 'a', 'r', nodes are created or reused. After last character 'r', node.isEnd is set to true.Step 2: Confirm isEnd flag meaning
isEnd marks the end of a complete word. Since 'car' was inserted, node.isEnd after 'r' is true.Final Answer:
Option D -> Option DQuick Check:
isEnd is true after inserting 'car' [OK]
Hint: isEnd is set true after inserting each complete word [OK]
Common Mistakes:
- Assuming isEnd is false if prefix overlaps with other words
2. Consider the following Python code implementing a bit trie for maximum XOR queries. Given
nums = [1, 2] inserted into the trie, what is the value of max_xor after calling max_xor(3)?easy
Solution
Step 1: Trace insertion of nums 1 and 2
Bits of 1 and 2 inserted from MSB to LSB into trie.Step 2: Trace max_xor(3)
For each bit of 3, try toggled bit in trie to maximize XOR. At highest differing bit, toggled bit exists, setting max_xor bits accordingly.Final Answer:
Option C -> Option CQuick Check:
3 XOR 0 = 3, 3 XOR 1 = 2, 3 XOR 2 = 1; max is 3 [OK]
Hint: XOR with 3 and 0,1,2 yields max 3 [OK]
Common Mistakes:
- Confusing bit order in trie traversal
- Off-by-one in bit indexing
- Returning wrong max_xor value
3. Consider the following code snippet implementing the optimal trie-based solution for shortest unique prefixes. Given the input words = ["dog", "dove", "duck"], what is the output of
shortest_unique_prefix(words)?easy
Solution
Step 1: Trace insertion frequencies
Insert "dog": d(1), o(1), g(1); "dove": d(2), o(2), v(1), e(1); "duck": d(3), u(1), c(1), k(1).Step 2: Find unique prefixes
For "dog": prefix "dog" (g freq=1) unique; for "dove": prefix "dov" (v freq=1) unique; for "duck": prefix "du" (u freq=1) unique.Final Answer:
Option D -> Option DQuick Check:
Prefixes match first node with freq=1 [OK]
Hint: Unique prefix ends where node freq equals 1 [OK]
Common Mistakes:
- Stopping prefix too early
- Assuming first letter always unique
4. You are given a 2D grid of letters and a list of words. You need to find all words from the list that can be formed by sequentially adjacent letters on the board, where adjacent letters are horizontally or vertically neighboring. Each letter cell may only be used once per word. Which algorithmic approach is best suited to efficiently solve this problem when the word list is large?
easy
Solution
Step 1: Understand problem constraints and prefix sharing
Multiple words need to be found on the board, and many words may share prefixes, making prefix pruning beneficial.Step 2: Use Trie for efficient prefix pruning during DFS
Building a Trie allows pruning DFS paths early if no word starts with the current prefix, avoiding redundant searches and improving efficiency.Final Answer:
Option A -> Option AQuick Check:
Trie + DFS reduces search space drastically compared to checking each word separately [OK]
Hint: Trie + DFS prunes invalid prefixes early [OK]
Common Mistakes:
- Thinking brute force DFS per word is efficient
- Assuming DP applies here
- Using sorting and binary search without prefix pruning
5. Examine the following buggy code snippet for building the trie and collecting suggestions. Which line contains the subtle bug that can cause incorrect suggestion ordering or runtime errors?
medium
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]
Hint: Always sort products before building trie for lex order [OK]
Common Mistakes:
- Forgetting to sort products
- Appending suggestions without limit
- Not handling missing prefix nodes
