← Back to index
EasyC3 min read

Number of Recent Calls

implement RecentCounter using circular queue to count requests within last 3000ms.

queuedesign

Problem link

View on LeetCode

Solutions in this repo

  • C../C/number-of-recent-calls/solution.cC solution

use circular queue to maintain sliding window of last 3000ms. on each ping, add timestamp and remove timestamps older than 3000ms. return queue size.

circular queue allows efficient enqueue and dequeue operations. maintain front and rear pointers, handle wraparound for circular behavior.

implementation

  • circular queue with fixed capacity
  • enqueue new timestamps
  • dequeue timestamps outside 3000ms window
  • return current queue size

complexity

O(1) amortized time per ping. O(1) space for queue (bounded by 3000ms window).