Put it all together
Wire the trie, the index and the speller into one engine: suggestions while typing, ranked results for a query, and when a word isn't known, an automatic correction with a "did you mean" message — exactly how real search boxes behave.
Build once, answer many
Finishing this engine is a good moment to look back: each piece was a lesson on its own, and together they make a feature people use every day.
createSearchEngine(docs) does all the expensive preparation up front: insert every word into the trie with its frequency, and build the inverted index. The returned object keeps them in a closure, so each keystroke and each query reuses the same structures. This is the same build-once idea as the leaderboard and the index, applied to the whole feature.
- Words: split every document once; reuse the list for both structures.
- Trie: insert each word with its total count across documents.
- Index: word → document id → count.
- Vocabulary for spelling: the trie's (or index's) keys.
If documents change, rebuild — or add update methods later. Start simple.