Implement a Stateful Search Autocomplete Session
Company: Pinterest
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
# Implement a Stateful Search Autocomplete Session
Build an autocomplete engine from historical sentences and their usage counts. While a user types a session, return up to three historical sentences that begin with the current prefix. Rank candidates by descending usage count, breaking ties by lexicographically smaller sentence. The order of the initial `sentences` input must not affect the result.
The character `#` ends the current sentence. Commit the nonempty accumulated text with one additional use, return an empty suggestion list for that character, and reset the prefix for a new session.
## Function Contract
```python
def run_autocomplete(
sentences: list[str],
frequencies: list[int],
keystrokes: str,
) -> list[list[str]]:
...
```
Return one suggestion list for each character in `keystrokes`.
## Constraints
- `len(sentences) == len(frequencies)`
- Initial sentences are distinct and nonempty.
- Sentences and typed characters contain lowercase English letters and spaces; `#` appears only in `keystrokes`.
- `1 <= frequencies[i] <= 10**6`
- At most 500 initial sentences and 500 keystrokes are supplied.
- If `#` is entered with an empty prefix, do not add an empty sentence.
## Example
```text
sentences = ["island", "ironman", "i love you", "i love leetcode"]
frequencies = [3, 2, 5, 2]
keystrokes = "i a#i"
```
The function returns a list of suggestions after each character, including an empty list at `#`, and the newly committed sentence participates in later prefixes.
Quick Answer: Implement a stateful autocomplete session that ranks matching historical sentences by frequency and lexicographic order. Process each keystroke, commit completed nonempty sentences, update future suggestions, and keep results independent of the initial input order.
Implement run_autocomplete(sentences, frequencies, keystrokes). After each ordinary character, return up to three historical sentences beginning with the current prefix, ranked by descending count then lexicographically. # commits the nonempty prefix with one additional use, returns an empty suggestion list, and resets the session. Return one suggestion list per keystroke.
Constraints
- len(sentences) == len(frequencies); initial sentences are distinct and nonempty.
- Sentences and ordinary keystrokes contain only lowercase English letters and spaces; # appears only in keystrokes.
- 1 <= frequencies[i] <= 1,000,000.
- At most 500 initial sentences and 500 keystrokes are supplied.
- Entering # with an empty prefix returns an empty list, resets the prefix, and does not store an empty sentence.
Examples
Input: ([], [], "a#")
Expected Output: [[], []]
Explanation: A new sentence has no suggestions before it is committed.
Input: (["a"], [2], "a#")
Expected Output: [["a"], []]
Explanation: An exact historical sentence is suggested, then committed.
Hints
- Keep sentence counts across # boundaries.
- Apply both ranking keys before taking three matches.