Practice/Ramp/Leetcode 1348. Tweet Counts Per Frequency
CodingMust
Design and implement an event analytics system that tracks timestamped events and provides aggregated counts over time intervals. Your system must support two primary operations:
The time granularities are defined as:
For a query with start time startTime and end time endTime, divide the inclusive range [startTime, endTime] into contiguous buckets of the specified size. Return a list containing the event count for each bucket in chronological order.
EventAnalytics with a constructor and two methodsrecordEvent(eventName, timestamp): Records that an event of type eventName occurred at the given timestamp (in seconds)getEventCountsPerFrequency(eventName, frequency, startTime, endTime): Returns a list of integers representing event counts per time bucket[startTime, endTime] inclusive1 <= eventName.length <= 500 <= timestamp, startTime, endTime <= 10^9startTime <= endTimefrequency is one of: "minute", "hour", "day"10^4 total calls to recordEvent and getEventCountsPerFrequencyExample 1:
` Operations: analytics = EventAnalytics() analytics.recordEvent("click", 10) analytics.recordEvent("click", 70) analytics.recordEvent("click", 130) result = analytics.getEventCountsPerFrequency("click", "minute", 0, 180)
Output: [1, 1, 1] Explanation:
Example 2:
` Operations: analytics = EventAnalytics() analytics.recordEvent("payment", 5) analytics.recordEvent("payment", 10) analytics.recordEvent("payment", 65) result = analytics.getEventCountsPerFrequency("payment", "minute", 0, 120)
Output: [2, 1, 0] Explanation:
Example 3:
` Operations: analytics = EventAnalytics() analytics.recordEvent("error", 1000) analytics.recordEvent("error", 5000) analytics.recordEvent("success", 2000) result1 = analytics.getEventCountsPerFrequency("error", "hour", 0, 7199) result2 = analytics.getEventCountsPerFrequency("success", "hour", 0, 7199)
Output: result1 = [2, 0] result2 = [1, 0] Explanation: Different event names are tracked independently `