Quant Memo
Coding/●●●●●

Count requests in the last five minutes

Build a counter over timestamped events (timestamps arrive non-decreasing):

  • record(t), log a request at time t.
  • count_recent(t), return how many requests fall in the trailing window (t - 300, t], that is, within the last 300 seconds.
record(1); record(100); record(3001); record(3002)
count_recent(3002)  -> 2     # only 3001 and 3002 are within (2702, 3002]

Make count_recent run in O(logn)O(\log n).

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions