As reported: Microsoft SDE (Noida, Dec 2024 hiring drive), round 2: given a stream of timestamps, return the data of the past 5 minutes; timestamps can be added in unsorted order. It resembles LeetCode Design Hit Counter (whose timestamps arrive in order), but here they do not.
Design HitCounter:
hit(timestamp)— record one hit attimestamp(seconds). Timestamps arrive in any order; several hits may share a timestamp.getHits(timestamp)— return the number of hits in the past 5 minutes up totimestamp: hits withtimestamp − 300 < t ≤ timestamp.
Query times are also in any order, and a query only counts hits recorded before it.
Example 1
hit(1); hit(2); hit(3)
getHits(4) -> 3
hit(300)
getHits(300) -> 4
getHits(301) -> 3 (the hit at 1 is now more than 300 seconds old)
Example 2
hit(500); hit(100); hit(320)
getHits(400) -> 1 (only 320; 100 is exactly 300 seconds old)
hit(450)
getHits(400) -> 1 (450 is after 400)
getHits(600) -> 3 (320, 450, 500)
getHits(99) -> 0
Constraints: 1 ≤ timestamp ≤ 10^9; up to 2 · 10^4 calls.
Code runner format: two lines in LeetCode's design format: the method names ("HitCounter", "hit", "getHits") and their arguments. The harness prints the list of return values (null for the constructor and hit). Go: Constructor(), Hit, GetHits.