STAQ-P3

Recent pings

Practice 3 of 6Stacks and queues6 tests

The problem

A signal box receives pings at increasing times. Write a class RecentPings(window). Its method ping(t) records a ping at time t, then returns how many pings so far arrived between t - window and t, both ends included.

r = RecentPings(3000)
r.ping(1)      # 1
r.ping(100)    # 2
r.ping(3001)   # 3: times 1 … 3001 hold all three
r.ping(3002)   # 3: times 2 … 3002; 1 is too old

Limits

  • Each call’s t is bigger than the one before.
  • Up to 300,000 calls; each one should take O(1) time on average.

Builds on STAQ-3 Queues and deque.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

6 tests, not run yet
Test results
#TestResultReads · time
1SmallNot run
2Edge of the windowNot run
3Long gapNot run
4Window 0Not run
5Two objectsNot run
6300,000 pingsNot run