Autocomplete component

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-05-07

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

•Capture user input dynamically as they type.
•Trigger search suggestions after each keystroke (with optional debounce/delay).

Suggestions Retrieval

•Based on the current prefix (typed text), fetch matching words from:
•Local cache / data structure (for speed)
•Or an external source (optional extension)

Display Suggestions

•Show the top N most relevant suggestions (e.g., 5–10 items).

Ranking / Sorting

•Sort suggestions based on:
•Frequency (most searched/popular first)
•Lexicographical order
•Context relevance (optional)

Selection

•When the user selects a suggestion, it becomes the final input.

Cache / Learning (Optional)

•Cache frequently used terms for faster lookups.
•Update ranking based on user selections.


🚫 Non-Functional Requirements


•Fast response time — suggestions should appear within 100–200 ms.
•Space efficiency — use compact data structures (e.g., Trie, HashMap).
•Scalability — should support large datasets (e.g., 1M+ keywords).
•Extensibility — easy to integrate ranking, caching, or API backends later.


🧱 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.System Initialization
1 2words = ["apple", "app", "application", "apply", "banana", "band", "bat"] 3autoCompleteSystem.load(words)
1.User Typing
1 2User types: "ap" 3→ Trie returns ["app", "apple", "application", "apply"] 4→ System displays top 3 suggestions
1.User Continues
1 2User types: "app" 3→ Suggestions updated: ["app", "apple", "application", "apply"]
1.User Selects
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


•Max number of stored words: 10^6
•Max prefix length: 100
•Max suggestions returned: configurable (default 5–10)
•Must support insertions and deletions dynamically (optional)


🎯 Goals


Your design should:


•Focus on OOP and data structure design (e.g., Trie, caching layer).
•Demonstrate extensibility (new ranking or data sources can be added easily).
•Keep search efficient (O(L) for lookup, where L = prefix length).
•Ensure clean separation between data, logic, and user interaction.


✅ 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.

Solution

Comments