This question evaluates proficiency in designing efficient prefix-matching and ranking mechanisms, including management of incremental query state and frequency-based ordering.
Design an autocomplete component that suggests the most relevant search phrases as a user types.
You are given historical sentences with their usage counts. The system receives the query one character at a time and must return suggestions after each character.
sentences[i]
: a previously typed sentence
times[i]
: how many times it was typed
c
:
c != '#'
, append it to the current query prefix and return the
top 3
matching historical sentences that start with this prefix.
c == '#'
, treat the current query as a completed sentence:
Implement a class with methods:
AutocompleteSystem(sentences, times)
input(c) -> List[str]
2 * 10^4
100
input()
calls: up to
2 * 10^4