Recent Event Window
Asked at Snowflake
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
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, afterx,y,xthe window has two distinct keys andxleads with count 2. Addingzevicts the firstx;y,x,zare all tied at 1, so the smallest keyxwins. A secondzmakeszthe leader. A thirdzevictsx, leavingz,z,zwith one distinct key.