Google VO 面试真题解析:10 秒窗口内去重消息显示

19次阅读
没有评论

Imagine you have a robot that sends status messages that humans will read in real time. The raw messages are hard to read for a human because there are often many messages produced in short periods of time. One idea is to make them more readable is to remove duplicate messages over a 10 second window.

Design and implement a program to hide duplicates of any message that has already been displayed within the past 10 seconds.

Example Messages Received, with Timestamps:

10 solar panel activated
11 low battery warning
12 tire one: low air pressure
13 solar panel activated
14 low battery warning
21 solar panel activated
35 solar panel activated

Example Messages Shown to User:

10 solar panel activated
11 low battery warning
12 tire one: low air pressure
21 solar panel activated
35 solar panel activated

From experience operating the robot, users have determined that there is a bug in the robot and duplicate messages are not to be trusted at all, and duplicate messages should be completely removed from the output if they occur within 10 seconds. Design and implement a program to completely remove the duplicate messages.

这道题要求你为机器人消息流做“10 秒内去重”:如果某条消息在过去 10 秒内已经显示过,那么当前这条就要被隐藏。关键做法通常是用哈希表记录每条消息最近一次被显示的时间戳,按时间顺序遍历消息时,判断当前时间与上次显示时间的差值是否大于等于 10 秒;满足条件才输出并更新记录,否则直接跳过。示例中像“solar panel activated”在 13 秒时因为 10 秒内已出现过,所以不显示,而 21 秒时距离上次显示已超过 10 秒,因此可以再次展示。

正文完
 0