Design Autocomplete
JavaScript
hard
30 mins
Design a class AutocompleteSystem that helps with word suggestions based on a given prefix.
You must implement the following methods:
insert(word: string): Inserts a word into the system.search(prefix: string): string[]: Returns all words in the system that start with the given prefix. The result can be in any order.
Example Inputs & Outputs
const system = new AutocompleteSystem(); system.insert('cat'); system.insert('car'); system.insert('carbon'); system.insert('dog'); system.search('ca'); // ['cat', 'car', 'carbon'] system.search('car'); // ['car', 'carbon'] system.search('do'); // ['dog'] system.search('z'); // []
Constraints & Edge Cases
- Words are lowercase English letters only.
- Prefixes can be empty strings — return all inserted words in that case.
- Duplicates may be inserted but only need to appear once in search results.
- Assume each
searchis case-sensitive. - Handle large number of insertions efficiently (use Trie, not brute force list scan).
Companies:
pinterest
zomato
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
