Per-Key Cooldown Filter for a Timestamped Event Feed
Data Structure DesignAuto-graded · 19 test casesLast updated
Walk a time-ordered event feed and decide which events pass a 10-second per-key cooldown, where only accepted events restart the cooldown.
Per-Key Cooldown Filter for a Timestamped Event Feed
A notification service receives a feed of events. Each event has a text label and the time (in whole seconds) at which it arrived. To avoid flooding the output, the service lets an event through only if its label has not already been let through within the last 10 seconds.
You are given two parallel lists, timestamps and messages, that describe the feed in arrival order. Return a list of booleans saying, for every event, whether it is let through (True) or dropped (False).
Rules
- Event
ihas arrival timetimestamps[i]and labelmessages[i]. Process events in the given order. - The first event with a given label is always let through.
- Suppose a label was most recently let through at time
t. A later event with the same label is let through only if its time is at leastt + 10. A gap of exactly 10 seconds is enough. - Only events that are let through affect the cooldown. A dropped event does not restart or extend the cooldown of its label.
- Different labels are completely independent. Several events may have the same timestamp.
- Two labels are the same only if the strings are exactly identical.
- The returned list has the same length as the input. Entry
iisTrueif eventiis let through andFalseotherwise.
Constraints
1 <= len(timestamps) == len(messages) <= 10^40 <= timestamps[i] <= 10^9, andtimestampsis non-decreasing1 <= len(messages[i]) <= 30- Each label consists only of lowercase English letters and digits
Examples
Example 1
Input: filter_events_by_cooldown([0, 4, 9, 10, 10, 15], ['disk', 'cpu', 'disk', 'disk', 'cpu', 'cpu'])
Output: [True, True, False, True, False, True]
Example 2
Input: filter_events_by_cooldown([5, 12, 15], ['a', 'a', 'a'])
Output: [True, False, True]
Hints
Hint 1
Each label needs only a single remembered value. Think about what that value should be.
Hint 2
Store the time at which each label was last let through, not the time it was last seen. What should happen to that stored value when an event is dropped?
Hint 3
Use a dictionary from label to last accepted time. An event passes if the label is missing or its time is at least the stored time plus 10; update the dictionary only when it passes.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each label needs just one piece of state: the time at which it was last let through. Keep a dictionary last_accepted that maps a label to that time, and scan the events in order:
- If the label is not in the dictionary, it has never been let through, so accept it and store its time.
- If the label is present and
t >= last_accepted[label] + 10, the cooldown has ended, so accept it and store the new time. - Otherwise drop the event and leave the dictionary unchanged.
The last point is the one that is easy to get wrong. If dropped events also updated the stored time, a label that keeps arriving every few seconds would never be accepted again. In the sample [5, 12, 15] with label "a", the event at 12 is dropped and the stored time stays 5, so the event at 15 passes because 15 >= 5 + 10.
Why it is correct
Labels are independent, so consider one label.
Invariant: before event i is processed, last_accepted[label] equals the time of the most recent accepted event with that label among events 0..i-1, or the label is absent if none was accepted.
- At the start the dictionary is empty, which matches the invariant.
- For an event with this label, the rules accept it exactly when no earlier accepted event exists or when its time is at least the last accepted time plus 10. That is exactly the condition checked. If accepted, it becomes the newest accepted event, so storing its time maintains the invariant. If dropped, the newest accepted event is unchanged, so leaving the dictionary alone maintains it.
- Events with other labels do not touch this label's entry.
Events sharing a timestamp need no special case: after the first one is accepted, the next one with the same label sees a gap of 0, which is less than 10, so it is dropped.
Complexity
- Time: O(n * L), where L <= 30 is the label length (hashing a string). Treating L as constant, this is O(n).
- Space: O(k * L) for the dictionary, where k is the number of distinct labels, plus O(n) for the output.
Reference solution
from typing import List
def filter_events_by_cooldown(timestamps: List[int], messages: List[str]) -> List[bool]:
# last_accepted[label] = time at which the label was most recently let through
last_accepted = {}
result = []
for t, label in zip(timestamps, messages):
if label not in last_accepted or t >= last_accepted[label] + 10:
last_accepted[label] = t
result.append(True)
else:
result.append(False)
return result