Design an Order Processing API with Time-Window Queries

viaLeetCode

Requirements: Design an API to process incoming order batches, where each batch contains multiple orders with different timestamps. The API should allow querying the number of orders placed in the last X minutes (up to 30 minutes).

Design: Maintain a fixed circular array of 30 buckets, each representing one minute. Each incoming order is placed into the bucket corresponding to its timestamp (order_timestamp mod 30). Before writing/reading, reset any buckets that have "aged out" (their timestamp is more than 30 minutes old) to zero. To answer "orders in the last X minutes", sum the counts of the relevant X buckets. Use a ReentrantLock (or per-bucket locks) to guard concurrent writes/reads across threads processing order batches simultaneously.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account