Recent Event Window

Asked at SnowflakePhone screen

Reported by the problem's creator, not verified

You are given a stream of events, each a timestamp ts and a string key key, processed by an EventWindow(m). Timestamps arrive in non-decreasing order and may repeat. The window keeps only the m most recent arrivals: when an event arrives and the window already holds m events, the oldest arrival leaves, regardless of its timestamp. When several keys are equally frequent, the lexicographically smallest key is the answer. Each test drives one EventWindow through a sequence of calls and checks every return value; the constructor's return is not recorded.

Examples

  1. Example 1

    Input: init = [3], operations = [["top",[]],["distinct",[]],["add",[1,"x"]],["add",[2,"y"]],["add",[3,"x"]],["distinct",[]],["top",[]],["add",[4,"z"]],["distinct",[]],["top",[]],["add",[5,"z"]],["top",[]],["add",[6,"z"]],["distinct",[]],["top",[]]]

    Output: [null,0,null,null,null,2,"x",null,3,"x",null,"z",null,1,"z"]

    Explanation: With m = 3, after x, y, x the window has two distinct keys and x leads with count 2. Adding z evicts the first x; y, x, z are all tied at 1, so the smallest key x wins. A second z makes z the leader. A third z evicts x, leaving z, z, z with one distinct key.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.