Design an autocomplete utility with prefix search, duplicate words and single-occurrence deletion
Quick Overview
Implement an autocomplete dictionary that stores duplicate words, deletes one occurrence at a time, counts a word, and returns every word with a given prefix in lexicographic order. It tests trie design, ordered traversal and branch pruning on delete.
Design an autocomplete utility with prefix search, duplicate words and single-occurrence deletion
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a high-performance autocomplete utility that supports real-time updates to its dictionary and fast prefix-based lookups.
The system should allow inserting words (possibly duplicates), deleting individual occurrences of words, searching for all words matching a given prefix (returned in lexicographic order), and counting how many times a specific word appears in the dictionary.
If a word has been inserted multiple times, it should appear that many times in search results, and each `delete` call removes only one occurrence.
Implement the following methods:
- `AutocompleteSystem()`: create an empty dictionary.
- `insert(word: str)`: insert a word into the autocomplete dictionary.
- `delete(word: str) -> bool`: delete one occurrence of a word from the dictionary. Returns `True` if the word was found and deleted, `False` otherwise.
- `search(prefix: str) -> List[str]`: return all words in the dictionary that start with the given prefix, sorted lexicographically. Duplicate insertions produce duplicate results.
- `count(word: str) -> int`: return the number of times a word exists in the dictionary.
Example 1:
```text
Input:
operations = ["AutocompleteSystem", "insert", "insert", "insert", "insert", "search", "search", "search", "delete", "search", "count"]
arguments = [[], ["apple"], ["app"], ["application"], ["apex"], ["ap"], ["app"], ["b"], ["apple"], ["ap"], ["apple"]]
Output:
[null, null, null, null, null, ["apex", "app", "apple", "application"], ["app", "apple", "application"], [], true, ["apex", "app", "application"], 0]
```
The system starts empty, then 'apple', 'app', 'application' and 'apex' are inserted. `search("ap")` returns `["apex", "app", "apple", "application"]`, `search("app")` returns `["app", "apple", "application"]`, and `search("b")` returns `[]`. `delete("apple")` returns `true`, after which `search("ap")` returns `["apex", "app", "application"]` and `count("apple")` returns 0.
Example 2:
```text
Input:
operations = ["AutocompleteSystem", "insert", "insert", "insert", "search", "delete", "delete", "search", "count"]
arguments = [[], ["cat"], ["cat"], ["car"], ["ca"], ["cat"], ["cat"], ["ca"], ["cat"]]
Output:
[null, null, null, null, ["car", "cat", "cat"], true, true, ["car"], 0]
```
'cat' is inserted twice and 'car' once, so `search("ca")` returns `["car", "cat", "cat"]`. The first `delete("cat")` returns `true` and removes one occurrence; the second returns `true` and removes the other. `search("ca")` then returns `["car"]`, and `count("cat")` returns 0.
```hint Shape of the index
Think about a structure in which every word sharing a prefix sits below one node, and what each node has to store so that repeated insertions of the same word are not lost.
```
```hint Order and cleanup
If you collect a node's own words before visiting its children, and visit the children in alphabetical order, what order do the results come out in? Also decide what `delete` should leave behind when a word's count drops to zero.
```
### Constraints & Assumptions
- `1 <= word.length <= 200`
- `1 <= prefix.length <= 200`
- All inputs consist of lowercase English letters only.
- At most 10,000 total calls are made across all methods.
### Clarifying Questions to Ask
- Should `search` return every match, or is there a cap on how many results to return?
- What should `delete` return for a word that was never inserted, or that exists only as the prefix of a longer word?
- Do searches greatly outnumber inserts and deletes? That decides whether caching results per prefix is worth it.
### What a Strong Answer Covers
- A prefix tree whose nodes store a count of word endings, so duplicates and `count` come from the same field.
- Producing lexicographic order from the traversal itself, without sorting the collected results afterward.
- A `delete` that removes exactly one occurrence, returns `False` for absent words and bare prefixes, and prunes branches that no longer lead to a word.
- Time and space complexity of each operation, including how `search` depends on the number and length of the matches.
### Follow-up Questions
- How would you return only the k most frequent matches for a prefix, and what would `search` cost then?
- If searches far outnumber updates, what would you cache, and how would inserts and deletes invalidate it?
- How would you make the structure safe for concurrent readers and writers?
- What changes for a dictionary with millions of words, or with characters outside lowercase English?
Overview: Implement an autocomplete dictionary that stores duplicate words, deletes one occurrence at a time, counts a word, and returns every word with a given prefix in lexicographic order. It tests trie design, ordered traversal and branch pruning on delete.
Design a high-performance autocomplete utility that supports real-time updates to its dictionary and fast prefix-based lookups.
The system should allow inserting words (possibly duplicates), deleting individual occurrences of words, searching for all words matching a given prefix (returned in lexicographic order), and counting how many times a specific word appears in the dictionary.
If a word has been inserted multiple times, it should appear that many times in search results, and each delete call removes only one occurrence.
Implement the following methods:
AutocompleteSystem()
: create an empty dictionary.
insert(word: str)
: insert a word into the autocomplete dictionary.
delete(word: str) -> bool
: delete one occurrence of a word from the dictionary. Returns
True
if the word was found and deleted,
False
otherwise.
search(prefix: str) -> List[str]
: return all words in the dictionary that start with the given prefix, sorted lexicographically. Duplicate insertions produce duplicate results.
count(word: str) -> int
: return the number of times a word exists in the dictionary.
The system starts empty, then 'apple', 'app', 'application' and 'apex' are inserted. search("ap") returns ["apex", "app", "apple", "application"], search("app") returns ["app", "apple", "application"], and search("b") returns []. delete("apple") returns true, after which search("ap") returns ["apex", "app", "application"] and count("apple") returns 0.
'cat' is inserted twice and 'car' once, so search("ca") returns ["car", "cat", "cat"]. The first delete("cat") returns true and removes one occurrence; the second returns true and removes the other. search("ca") then returns ["car"], and count("cat") returns 0.
Constraints & Assumptions
1 <= word.length <= 200
1 <= prefix.length <= 200
All inputs consist of lowercase English letters only.
At most 10,000 total calls are made across all methods.
Clarifying Questions to Ask Guidance
Should
search
return every match, or is there a cap on how many results to return?
What should
delete
return for a word that was never inserted, or that exists only as the prefix of a longer word?
Do searches greatly outnumber inserts and deletes? That decides whether caching results per prefix is worth it.
What a Strong Answer Covers Guidance
A prefix tree whose nodes store a count of word endings, so duplicates and
count
come from the same field.
Producing lexicographic order from the traversal itself, without sorting the collected results afterward.
A
delete
that removes exactly one occurrence, returns
False
for absent words and bare prefixes, and prunes branches that no longer lead to a word.
Time and space complexity of each operation, including how
search
depends on the number and length of the matches.
Follow-up Questions Guidance
How would you return only the k most frequent matches for a prefix, and what would
search
cost then?
If searches far outnumber updates, what would you cache, and how would inserts and deletes invalidate it?
How would you make the structure safe for concurrent readers and writers?
What changes for a dictionary with millions of words, or with characters outside lowercase English?