Inspyrsolutions OA 面试真题解析:按门店聚合事件生成健康摘要

52次阅读
没有评论

def aggregate_store_events(events: list[dict]) -> dict:

Aggregate a list of store events into a per-store health summary.

Each event dict has:

  • store_id (str)
  • type (str)
  • severity (int, 1 to 5)
  • ts (int, Unix timestamp)

Returns a dict keyed by store_id, each value containing:

  • event_count (int): total events for that store
  • max_severity (int): highest severity seen
  • latest_ts (int): most recent event timestamp
  • flagged (bool): True if any event has severity >= 4

这道题要求你把同一门店的事件按 <code>store_id</code> 汇总成健康摘要。核心做法是用一个哈希表 / 字典遍历所有事件,针对每个门店维护四个字段:事件总数 <code>event_count</code>、最高严重级别 <code>max_severity</code>、最近时间戳 <code>latest_ts</code> 和是否被标记 <code>flagged</code>。遍历时每读到一条事件就将计数加一,使用 <code>max</code> 更新最高严重级别和最新时间戳,并在 <code>severity &gt;= 4</code> 时把 <code>flagged</code> 置为真。这个题考察的是字典分组聚合、单次遍历维护状态以及对简单布尔条件的处理,整体思路清晰,时间复杂度为线性。

正文完
 0