Autocomplete top 3
Build an autocomplete index from a list of (word, frequency) pairs. Then for each query prefix, return up to 3 completions that start with that prefix, ranked by frequency descending; break ties by lexicographic order of the word ascending. Process the queries in order and return a list of result lists (each a list of words). A query with no matching word yields an empty list.
Implement
autocomplete(words: list[list], queries: list[str]) → list[list[str]]Examples
in
[[["cat",5],["car",9],["card",2],["dog",7]],["ca","do","x"]]out[["car","cat","card"],["dog"],[]]What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 30 min
solution.py
InputExpectedGot
[[["cat",5],["car",9],["card",2],["dog",7]],["ca","do","x"]][["car","cat","card"],["dog"],[]]not run yetsample