📌 Problem Statement
Imagine you have an RPC server that produces log entries and we’re analyzing it offline.
There are two entries for each call:
- one when the RPC starts,
- and one when the RPC finishes processing.
Your task is to efficiently determine if any RPC in the log timed out.
You can assume your function receives two arguments:
- A timeout duration (integer).
- A collection of pre-parsed log entries, already sorted by timestamp.
Example
Input timeout:
3
Input log:
| ID | Timestamp | Event Type |
|---|---|---|
| 1 | 0 | Start |
| 2 | 1 | Start |
| 2 | 6 | End |
| 3 | 6 | Start |
| 1 | 7 | End |
| 3 | 8 | End |
Graphical representation of the log
ID: 1 (------------)
ID: 2 (-----------)
ID: 3 (------)
Time: 0 1 2 3 4 5 6 7 8
Timeout = 3
Check whether any RPC ran longer than 3 units.
思路非常清晰:
- 遍历日志,维护一个
start_time哈希表:id → startTimestamp - 遇到 Start 事件,记录开始时间
- 遇到 End 事件:
- 计算:
duration = endTimestamp - startTimestamp[id] - 若
duration > timeout→ 立刻返回 True(发生超时)
- 计算:
- 全部处理完仍没有超时 → 返回 False
日志已按时间排序 → 不需要排序,提高效率。
时间复杂度:O(n)
空间复杂度:O(n)(最坏情况所有 RPC 都未结束)
VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 Meta、Google、Amazon 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备 Stripe 或类似工程向公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。