Editor

Hit Counter

#67 · Classes · Intermediate · 65 XP · about 12 min

A web page wants to show how busy it is. Build a HitCounter class that records hits and reports how many happened in the last 5 minutes.

  • HitCounter() starts with no hits.
  • hit(t) records one hit at second t.
  • count(t) returns the number of hits in the 300 seconds ending at second t: those with a time from t - 299 to t, both included.

Times are whole numbers of seconds and never go down from one call to the next (across both methods). Several hits can share the same second.

Examples

c = HitCounter()
c.hit(1)
c.hit(2)
c.hit(3)
c.count(4)      # returns 3
c.hit(300)
c.count(300)    # returns 4
c.count(301)    # returns 3    (the hit at second 1 is now too old)

Constraints

  • 1 <= t <= 2,000,000,000
  • Up to 10,000 calls in total
  • A hit that is too old never counts again, so there is no need to keep it.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.