Magicsheet logo

Number of Recent Calls

Easy
45.8%
Updated 6/1/2025

Number of Recent Calls

What is this problem about?

The Number of Recent Calls problem asks you to design a class that accepts timestamp-ordered ping(t) calls and returns the count of pings in the last 3000 milliseconds (inclusive) — i.e., in the range [t-3000, t]. This Number of Recent Calls coding problem is a classic sliding window queue design problem.

Why is this asked in interviews?

Databricks, Uber, Microsoft, Meta, Amazon, Google, and Bloomberg ask this as a system design + data structures warm-up. It tests whether candidates know that a queue (deque) is the right data structure for a sliding time window. The data stream, design, and queue interview pattern is directly applied.

Algorithmic pattern used

Queue with front-pruning. Maintain a queue of all ping timestamps. For each new ping t: add t to the back. Remove all timestamps from the front that are less than t - 3000. Return the current queue size.

Example explanation

Pings: [1, 100, 3001, 3002].

  • ping(1): queue=[1]. No removal. Size=1.
  • ping(100): queue=[1,100]. Remove t<100-3000=-2900, nothing. Size=2.
  • ping(3001): queue=[1,100,3001]. Remove t<1, nothing. Size=3.
  • ping(3002): queue=[1,100,3001,3002]. Remove t<2, remove 1. Queue=[100,3001,3002]. Size=3.

Common mistakes candidates make

  • Using an array with pop(0) instead of a deque (O(n) vs O(1) for front removal).
  • Off-by-one: the window is [t-3000, t] (inclusive on both ends).
  • Not removing all outdated pings from the front (only removing one).
  • Implementing from scratch without knowing deque is the right structure.

Interview preparation tip

Sliding window data stream problems always use a deque or queue with front-removal. The pattern: (1) add new element to back, (2) remove expired elements from front, (3) return size. This applies to moving averages, recent activity counts, and real-time monitoring. Practice designing Rate Limiter, Moving Average, and Request Counter — they all use this same queue-with-expiry template.

Similar Questions