Autocomplete component
Problem Statement
Design the Low-Level Design (LLD) of an Autocomplete component — like the search suggestion box in Google, Amazon, or LinkedIn.
The component should dynamically suggest possible completions as the user types in a query, based on a preloaded dataset or API calls.
Your focus is on object-oriented modeling of how the component works internally — data structures, classes, and interactions — not on UI or network optimization (that would belong to
HLD).
🧠 Functional Requirements
Input Handling
Suggestions Retrieval
Display Suggestions
Ranking / Sorting
Selection
Cache / Learning (Optional)
🚫 Non-Functional Requirements
🧱 Suggested Classes
ClassResponsibility
AutoCompleteSystem
Main class managing input handling and suggestion generation.
Trie
Efficient prefix-based data structure for storing and searching suggestions.
TrieNode
Represents a node in the Trie (character, children, end-of-word flag, frequency).
Suggestion
Represents a suggestion with text and ranking score.
Cache
Optional component to store recently queried prefixes and their results.
RankingStrategy (Interface)
Defines how suggestions are ranked (e.g., frequency, recency).
FrequencyBasedRanking, LexicalRanking
Implementations of ranking strategies.
InputHandler
Handles keystrokes and triggers autocomplete updates (with optional debounce).
🔄 Example Flow
1
2words = ["apple", "app", "application", "apply", "banana", "band", "bat"]
3autoCompleteSystem.load(words)1
2User types: "ap"
3→ Trie returns ["app", "apple", "application", "apply"]
4→ System displays top 3 suggestions1
2User types: "app"
3→ Suggestions updated: ["app", "apple", "application", "apply"]1
2User selects: "application"
3→ Cache stores ("app" → ["application", "apple", "apply"])
4→ Frequency of "application" incremented🧪 Example Input / Output
Input:
1
2Dicti ["dog", "door", "dorm", "cat", "car", "cart", "care"]
3Prefix = "do"Output:
1
2Suggestions: ["dog", "door", "dorm"]Input:
1
2Prefix = "car"Output:
1
2Suggestions: ["car", "cart", "care"]⚙️ Constraints
🎯 Goals
Your design should:
✅ In short:
Design an Autocomplete (Typeahead) system that efficiently stores and retrieves suggestions based on user input prefixes, with ranking, caching, and modular extensibility using clean OOP design.