Code Room
CodingMediumcod-g346
Subject QueuesLevel Mid–Senior~20 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Design a hit counter that counts hits in the most recent 300 seconds (a 5-minute sliding window). Process ops: ['hit', timestamp] records a hit at the given second (timestamps are non-decreasing), and ['getHits', timestamp] returns the number of hits in the window (timestamp-299 .. timestamp] inclusive, i.e. strictly more recent than timestamp-300. Return the list of getHits results.

Implement
hit_counter(ops: list[list]) → list[int]
Examples
in[[["hit",1],["hit",2],["hit",3],["getHits",4],["hit",300],["getHits",300],["getHits",301]]]out[3,4,3]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.