← Problems27. Top K events in a streamHardPython
00:00 / 20:00

Top K events in a stream

Hard·Acceptance ·Asked at Swiggy, Flipkart

Return the K most frequent event types from a stream you may only iterate once, without holding every event in memory.

Input schema

solve(events: Iterator[str], k: int) -> list[tuple[str, int]]

Example

events -> "click", "view", "click", "buy", "click", "view" k = 2 Output = [("click", 3), ("view", 2)]

Constraints

  • Ties on count break on event name ascending
  • k may exceed the number of distinct events; return everything then
  • Distinct event types stay under 100,000 but the stream is 800,000,000 events
  • Sorting the whole stream fails the time limit

Topics

aggregation

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.py
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8 · ⌘↵ run, ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.