Implement Trie Insert and Exact-Word Search
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Implement trie insertion and exact-word search for up to a million total characters, distinguishing terminal words from mere prefixes and returning search results in operation order.
Read the full Google Software Engineer interview experience this question came from
Constraints
- operations contains at most 200000 encoded insert or search lists.
- Every word has length between 1 and 1000.
- The total number of characters across all operations is at most 1000000.
- Words contain only lowercase English letters.
- Duplicate insertion has no additional effect.
- Only search operations contribute returned booleans.
Examples
Input: ([['insert', 'apple'], ['search', 'apple'], ['search', 'app'], ['insert', 'app'], ['search', 'app']],)
Expected Output: [True, False, True]
Explanation: This is the source example; app is only a prefix until it is explicitly inserted.
Input: ([],)
Expected Output: []
Explanation: No search operations produce an empty result.
Hints
- Let every trie node represent one consumed prefix.
- Mark a node separately when an inserted word ends there.