Requirements
Implement a class for trading-engine clients that enforces order-message rate limits over a continuous moving time window.
The class must support:
- Configurable limits, such as
100order messages per1 secondor500order messages per1 minute. - A true moving window measured against the past
timeLimitWindow, not a coarse fixed bucket. - Batch sends: the caller may ask to send more than one message at a timestamp.
- A method shaped like
RateLimit(maxMsgCount, timeLimitWindow)andcheckRateLimit(int suggestOrders, time_unit_t insert_ts). - Unit tests covering normal acceptance, partial acceptance, expiry of old events, and requests larger than remaining capacity.
Examples
For RateLimit(50, 1000ms):
t = 0, suggest 20 -> allow 20
t = 50, suggest 30 -> allow 30
t = 300, suggest 10 -> allow 0
t = 1000, suggest 30 -> allow 20
t = 1050, suggest 7 -> allow 7Notes
The direct implementation is a deque of (timestamp, accepted_count) events. Before each request, evict entries with timestamp <= now - window. Sum the remaining accepted counts, compute remaining = max(0, limit - used), allow min(suggestOrders, remaining), and append the accepted amount if it is positive.
If timestamps are nondecreasing, eviction is O(1) amortized and each request is O(1) amortized plus the cost of maintaining the running sum. If timestamps can arrive out of order, switch to an ordered map or reject nonmonotonic input explicitly.
Production extensions usually probe atomicity. In a multithreaded caller, the prune-count-add sequence must be protected by a lock or executed atomically inside the backing store; otherwise two callers can both observe remaining capacity and exceed the limit.
Preparation
- Implement the deque + running-sum version in C++ and Python, then write tests for the exact sample above.
- Practice explaining why a fixed-window counter is wrong at boundaries and why a sliding-window log is exact but memory grows with accepted events.
- Add one concurrency answer: a single mutex for in-process correctness, or an atomic server-side script if the state lives in Redis.

