Skip to the code
cf.
+10 XP

#258 Count Pings in the Last Three Seconds

easy

A status page shows how many health-check pings arrived recently. Write a class PingCounter:

  • new PingCounter() starts with no pings.
  • ping(t) records a ping at time t in milliseconds and returns how many pings happened in the last 3000 milliseconds, counting both ends: every ping with a time from t - 3000 to t, including this one.

Each call's t is strictly larger than the previous call's.

Each example lists the calls in order, then each call's arguments. The first call creates the object with new PingCounter(…); the output lists what each call returns (null for the constructor and for methods that return nothing).

Example 1

Input: ["PingCounter","ping","ping","ping","ping"]
[[],[1],[100],[3001],[3002]]
Output: [null,1,2,3,3]

At 3001 the range is 1 to 3001, so all three pings count. At 3002 the ping at 1 is too old.

Example 2

Input: ["PingCounter","ping"]
[[],[5000]]
Output: [null,1]

The first ping is the only one.

Constraints

  • 1 <= t <= 10^9
  • At most 10^4 calls.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).