Lucid Motors Lucid 电面 Coding|遥测消息滑窗去重
Autonomous vehicles continuously stream telemetry event messages (e.g., speed, tire pressure, system status updates) …
2026-07OAVO Helper · 面经整理
01 · QUESTIONS
面试题目
- Autonomous vehicles continuously stream telemetry event messages (e.g., speed, tire pressure, system status updates) to a cloud endpoint. Due to vehicle network retries and distributed message delivery protocols, duplicate messages frequently arrive at our ingestion worker. To optimize cloud storage costs and prevent corrupting downstream analytics pipelines, you must write a processing utility that filters out duplicate data blocks within a sliding time window.
- 要求: - Implement a class named TelemetryDeduplicator that processes messages. - Implement process_message(timestamp: int, message_id: str, payload: dict) -> bool. Returns True if the message is unique and should be kept, False if it is a duplicate and should be dropped. A message is a duplicate if its exact message_id has already been processed within the last 10,000 milliseconds (10 seconds) relative to the current message's timestamp. - Implement a clean-up mechanism to prevent memory leaks. The system should automatically purge tracking entries for expired message_ids that fall outside the 10-second window.
02 · FOLLOW-UPS
追问
- 清理逻辑所在位置
- 清理是否误删较新记录
- 乱序到达
- 多个键同时乱序
- 时间差极大的情形
- 测试写法
03 · ANSWERS
参考答案
实现 TelemetryDeduplicator:哈希表记录每个 message id 最近一次被保留的时间,判断时间差是否小于 10 秒;用按时间排序的小顶堆清理过期条目,防止内存泄漏。
解题思路
- 判重:与上次保留时间的绝对差小于窗口即为重复;被丢弃的消息不刷新时间。
- 清理放在
process_message内部,每次处理消息时顺带执行,不依赖外部定时器;也可以按需改成后台定时任务。 - 水位线 = 见过的最大时间戳 - 窗口,堆顶早于水位线的条目被弹出;删除 map 前核对是否仍是最新记录。
代码
import heapq
class TelemetryDeduplicator:
def __init__(self, window_ms=10_000):
self.window = window_ms
self.last_kept = {} # message_id -> 最近一次被保留的时间戳
self.expiry = [] # (时间戳, message_id) 小顶堆,用于清理
self.max_ts = None # 见过的最大时间戳(水位线)
def process_message(self, timestamp, message_id, payload=None):
self.max_ts = timestamp if self.max_ts is None else max(self.max_ts, timestamp)
self._cleanup()
prev = self.last_kept.get(message_id)
if prev is not None and abs(timestamp - prev) < self.window:
return False # 窗口内重复(乱序到达也按绝对差判断)
if prev is None or timestamp > prev:
self.last_kept[message_id] = timestamp
heapq.heappush(self.expiry, (timestamp, message_id))
return True
def _cleanup(self):
cutoff = self.max_ts - self.window
while self.expiry and self.expiry[0][0] <= cutoff:
ts, mid = heapq.heappop(self.expiry)
if self.last_kept.get(mid) == ts: # 只删仍是最新记录的条目
del self.last_kept[mid]复杂度
- 单条消息:均摊 O(log n)。
- 空间:O(窗口内不同 message id 的数量)。
追问解答
清理逻辑所在位置
- 放在
process_message开头:每处理一条消息就按水位线清理一次,内存随窗口自动收敛,不需要额外线程。 - 如果消息流可能长时间停顿,又需要释放内存,可以再加一个按需调用的
cleanup()或后台定时任务。
清理是否误删较新记录
- 同一个 id 在堆中可能有多条记录;只删除与 map 中时间戳一致的那一条,其余是过期的旧记录,只丢弃堆项。
乱序到达
- 用绝对差判重;晚到的旧消息不能覆盖 map 中更新的时间戳,否则会缩短后续的判重窗口。
多个键同时乱序
- 每个 id 独立判断,互不影响;清理用全局水位线,堆按时间排序,弹出顺序与 id 无关,所以多个键同时乱序也不会互相干扰。
时间差极大的情形
- 一条时间戳异常大的消息会推高水位线,导致其他 id 的记录被提前清理,之后的旧重复消息会被当作新消息保留。
- 需要先和面试官确认可容忍的乱序范围;可以只在时间戳合理时推进水位线,或者对异常时间戳单独记录告警。
测试写法
- 用确定的时间戳直接调用,不依赖真实时钟:窗口内重复、恰好在边界上(相差正好等于窗口)、窗口外再次出现、乱序、清理后 map 变小、旧记录不误删。
联系 OAVO Helper
想针对这题做模拟练习?
加微信咨询陪跑,我们可以按你的目标岗位安排简历、模拟面试或代投。
加微信咨询陪跑微信:aasummer