Code Room
CodingMediumcod-g674
Subject Stream processingLevel Mid–Senior~30 minCommon in Distributed systems · Algorithms & data structures interviewsIndustries Software development, Technology

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.