Event counter over a trailing 15-minute window with a ring buffer
Build an event counter for a monitoring system with two operations: record(timestamp) notes that an event happened at a given second, and count(now) returns how many events happened in the 15 minutes ending at now (later than now - 900, up to and including now).
Memory must stay fixed no matter how many events arrive, so use a ring buffer of per-second buckets. Handle many events in the same second, long gaps between calls, and stale buckets that must be cleared before reuse.
No answer yet. Write the first one; the best answer gets endorsed.
