Top-k frequent events stream

Write codeMedium30 minDatabricks style

Your task

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.

Examples

stream_topk(ops: list[list]) -> list[list[str]]
Example 1
Inputstream_topk([["add", "a"], ["add", "b"], ["add", "a"], ["topk", 2]])
Output[["a", "b"]]