Preparing your design workspace

Loading the problem and your saved attempt…

Design and Implement an In-Memory Tally Service

Design a thread-safe, bounded-memory counter that records timestamped events and answers exact inclusive range queries quickly.

Design and sketch the core implementation of an in-memory Tally Service. The public API exposes bump(timestamp), which records one count at that timestamp, and query(startTime, endTime), which returns the cumulative count in the inclusive range [startTime, endTime].

Discuss thread safety and performance under high concurrency, bound memory with a sliding window or time buckets instead of retaining unbounded history, and avoid scanning individual events for every query.

For this exercise, assume one service instance owns one counter in one process; persistence, replication, distributed aggregation, multiple named counters, and event payloads are out of scope. Timestamps are non-negative integer seconds, an injected clock defines the current second, and future timestamps are rejected.

The service has a positive configurable retention window and guarantees exact counts only for operations fully inside that retained window; out-of-window operations return an explicit error rather than a partial result. query rejects startTime greater than endTime.

No completed bump may be lost: a bump that finishes before a query begins must be reflected, while overlapping operations may take effect in either order as long as that order is consistent.