Dynamic Prefix Search Collection
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported this one in September 2026, and the hint says binary search, but the real hinge is how you store the words. A dynamic collection with INSERT and SEARCH prefix, returning the three smallest matches in lexicographic order. If your OA lands in the next day or two, expect up to 100000 operations, so a naive scan of every stored word on each search will die. Pick the structure first, then the code writes itself. StealthCoder sits invisibly on your screen as a safety net if you blank on the structure mid-assessment.
The problem
Implement a dynamic collection of lowercase words. Process the finite array operations from left to right. Each operation has one of these forms: INSERT word: add word to the collection. Inserting a word that is already present does not create a duplicate. SEARCH prefix: return up to three stored words that begin with prefix, ordered lexicographically. The collection starts empty. Return one row for every SEARCH operation, in command order. INSERT operations do not produce output. Function processPrefixSearch(operations: String[]) → String[][] Examples Example 1 operations = ["INSERT mouse","INSERT mobile","INSERT moneypot","INSERT monitor","INSERT mousepad","SEARCH mo","SEARCH mou"] return = [["mobile","moneypot","monitor"],["mouse","mousepad"]] The first search keeps the three lexicographically smallest matches for mo. The longer prefix mou has only two matches. Example 2 operations = ["INSERT app","INSERT apple","SEARCH ap","INSERT apex","INSERT app","SEARCH ap","SEARCH z"] return = [["app","apple"],["apex","app","apple"],[]] The second insertion of app is idempotent. After apex is added, it is first lexicographically. No stored word begins with z. Constraints 1 <= operations.length <= 100000. Every operation is exactly one documented command with one separating space. Every word and prefix contains 1 to 100 lowercase English letters. The total number of characters across all operations is at most 1000000. At least one operation is SEARCH.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Two clean approaches. First, a trie where each node keeps a list of the three smallest words passing through it. On INSERT, walk the word's characters, and at each node merge the word into that node's top-3 list, skipping duplicates. SEARCH is then just walking the prefix and returning that list. Cost is about 100 steps per operation. Second, keep a sorted list of unique words, use binary search (bisect left) on the prefix, then read up to three entries while they still start with the prefix. Insertion into a sorted array is O(n) in shifting, which can hurt at this scale. The pitfall is duplicates. Example 2 inserts app twice, so check a set or the node list before adding. Also return an empty list, not nothing, when no match exists. If you freeze, StealthCoder can hand you the trie version live.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Dynamic Prefix Search Collection cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as search suggestions system. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Dynamic Prefix Search Collection FAQ
What's the trick in the Amazon Dynamic Prefix Search problem?+
Store the top three words at every trie node. Each INSERT updates the nodes along the word's path, and each SEARCH walks the prefix and returns that node's list. No scanning, no sorting at query time.
Is binary search actually the right approach here?+
It can work. Keep a sorted unique list, bisect to the first word >= prefix, then take up to three that start with it. The catch is that inserting into the middle of an array is slow at 100000 operations, so the trie is safer.
How do I handle duplicate inserts?+
Treat INSERT as idempotent. With a trie, check whether the word is already in a node's top-3 list before adding. Alternatively, keep a global set of seen words and skip the insert entirely when the word is present.
What edge cases should I test before submitting?+
A prefix with no matches must return an empty list. A prefix equal to a full word should include that word. Fewer than three matches should return fewer rows. Also test a word inserted later that sorts ahead of existing ones, like apex in Example 2.
How do I prepare in 48 hours for this kind of question?+
Write a trie from scratch twice, then add the per-node top-3 list. Practice the bisect variant too so you have a fallback. Count complexity against the 1000000 total character limit so you can justify your choice quickly.