Design an autocomplete suggestions API
Problem: Autocomplete Suggestions API
Design an API for an autocomplete feature that provides word suggestions based on a given prefix.
Input
- A dictionary of
nunique words,words[i], composed of lowercase English letters (a–z). - An integer array
freq[i], wherefreq[i]represents the usage count forwords[i]. - Subsequently,
qqueries will be made. Each query consists of:- A
prefixstring. - An integer
k, indicating the maximum number of suggestions requested.
- A
Output
For each query, the API should return up to k suggested completions. The suggestions must adhere to the following ranking criteria:
- A completion must be any word that starts with the given
prefix. - Suggestions should be ranked primarily by higher frequency first.
- In case of a tie in frequency, suggestions should be ranked by lexicographical order.
Follow-up: Support Updates
Implement an add(word, delta) operation:
- This operation increases the frequency of
wordbydelta. - If
wordis new, it should be added to the dictionary with the specifieddeltafrequency. - All subsequent queries must reflect these updated frequencies.
Constraints (Reasonable Interview Assumptions)
1 ≤ n, q ≤ 2e5(number of words, number of queries)1 ≤ |word|, |prefix| ≤ 50(length of words and prefixes)- The total number of characters across all words and queries can be very large. Solutions should be significantly more efficient than scanning all words for each query.
Explain the data structure(s) you would use (e.g., Trie) and implement the required operations.
AI interview prep
Core dilemma
Balancing efficient prefix-based search with dynamic frequency-based ranking and updates, all while managing memory for a potentially large dictionary.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Design an autocomplete suggestions API
Problem: Autocomplete Suggestions API
Design an API for an autocomplete feature that provides word suggestions based on a given prefix.
Input
- A dictionary of
nunique words,words[i], composed of lowercase English letters (a–z). - An integer array
freq[i], wherefreq[i]represents the usage count forwords[i]. - Subsequently,
qqueries will be made. Each query consists of:- A
prefixstring. - An integer
k, indicating the maximum number of suggestions requested.
- A
Output
For each query, the API should return up to k suggested completions. The suggestions must adhere to the following ranking criteria:
- A completion must be any word that starts with the given
prefix. - Suggestions should be ranked primarily by higher frequency first.
- In case of a tie in frequency, suggestions should be ranked by lexicographical order.
Follow-up: Support Updates
Implement an add(word, delta) operation:
- This operation increases the frequency of
wordbydelta. - If
wordis new, it should be added to the dictionary with the specifieddeltafrequency. - All subsequent queries must reflect these updated frequencies.
Constraints (Reasonable Interview Assumptions)
1 ≤ n, q ≤ 2e5(number of words, number of queries)1 ≤ |word|, |prefix| ≤ 50(length of words and prefixes)- The total number of characters across all words and queries can be very large. Solutions should be significantly more efficient than scanning all words for each query.
Explain the data structure(s) you would use (e.g., Trie) and implement the required operations.