easyTriesAI-applied~16 min
Prefix Tree Index For Prompt Snippets
A prompt-engineering tool autocompletes saved prompt snippets as the user types. Build the prefix index it relies on: insert snippets, check exact matches, and test whether any snippet starts with a given prefix.
Problem
Implement a trie (prefix tree) supporting insert(word), search(word) — true only if the exact word was inserted — and startsWith(prefix) — true if any inserted word has that prefix.
Input
A sequence of insert/search/startsWith calls with lowercase strings (length ≤ 100).
Output
search and startsWith each return a boolean.
Constraints
- search matches whole words; startsWith matches any prefix
- Lowercase English letters only
- Up to 3·10^4 operations
Examples
Example 1
Input
insert("apple"), search("apple"), search("app"), startsWith("app")Output
search→true, search→false, startsWith→true
'app' is a prefix but was not inserted as a word.
Example 2
Input
insert("app"), search("app")Output
search→true
After inserting 'app' the exact search succeeds.