Filter Repeated Robot Status Messages
GoogleSoftware Engineer · Technical ScreenLast updated
Design an algorithm to rate-limit and deduplicate a stream of robot status messages so identical messages only display if strictly more than ten seconds have elapsed since their last displayed occurrence.
Problem Statement
You are monitoring a fleet of autonomous warehouse robots. Each robot emits diagnostic status messages as events occur. Because a robot may repeatedly report the same status during a fault condition, the dashboard needs to rate-limit messages to prevent alert fatigue.
Given a list of log entries where each entry consists of an integer timestamp (in seconds) and a string message, return a list of entries that should be displayed according to the following rules:
- A message is displayed if it has never been displayed before, or if strictly more than
10seconds have passed since its last displayed occurrence (current_timestamp - last_displayed_timestamp > 10). - Hidden (suppressed) messages must not update the last displayed timestamp.
- Distinct messages are tracked independently and are case-sensitive.
- The output must preserve the original relative order of the accepted messages.
Function Signature
def filter_robot_logs(logs: list[tuple[int, str]]) -> list[tuple[int, str]]:
pass
Examples
Example 1
logs = [
(1, "LOW_BATTERY"),
(5, "LOW_BATTERY"),
(11, "LOW_BATTERY"),
(12, "LOW_BATTERY")
]
# Output: [(1, "LOW_BATTERY"), (12, "LOW_BATTERY")]
Explanation:
- At
t = 1,"LOW_BATTERY"is displayed. Last displayed time is1. - At
t = 5,5 - 1 = 4 10, displayed. Last displayed time is updated to12.
Example 2
logs = [
(0, "OBSTACLE_DETECTED"),
(3, "Path_Blocked"),
(4, "PATH_BLOCKED"),
(10, "OBSTACLE_DETECTED"),
(11, "OBSTACLE_DETECTED")
]
# Output: [
# (0, "OBSTACLE_DETECTED"),
# (3, "Path_Blocked"),
# (4, "PATH_BLOCKED"),
# (11, "OBSTACLE_DETECTED")
# ]
Explanation:
"Path_Blocked"and"PATH_BLOCKED"are distinct messages because matching is case-sensitive.- For
"OBSTACLE_DETECTED",10 - 0 = 10is suppressed, but11 - 0 = 11 > 10is displayed.
Constraints
1 <= len(logs) <= 10^50 <= timestamp <= 10^9logsis sorted in non-decreasing order oftimestamp.1 <= len(message) <= 100messageconsists of printable ASCII characters.
Hints
Hint 1
Use a hash map to associate each unique message string with the timestamp at which it was last accepted and displayed.
Hint 2
Be careful to update the stored timestamp in the map only when an entry is displayed, not when it is suppressed.
Hint 3
Ensure you check that the difference is strictly greater than 10 (`timestamp - last_displayed > 10`), so a difference of exactly 10 is still filtered out.
Follow-up questions
- How would you prevent memory from growing unbounded if the stream runs continuously for days or months?
- How would you handle this problem if logs could arrive slightly out-of-order due to network latency?
- How would you scale this design if status messages were ingested concurrently across multiple worker instances?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.