Top-k frequent events stream
You consume a stream of string events and must answer top-k queries interleaved with the events. Given a list of operations, each either ['add', event] which records one occurrence of `event`, or ['topk', k] which asks for the k most frequent events seen so far, produce a result for each 'topk' op. Ties are broken alphabetically (smaller string first). Each 'topk' result is a list of event strings, most frequent first. Return the list of results in query order.
Implement
stream_topk(ops: list[list]) → list[list[str]]Examples
in
[[["add","a"],["add","b"],["add","a"],["topk",2]]]out[["a","b"]]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
[[["add","a"],["add","b"],["add","a"],["topk",2]]][["a","b"]]not run yetsample