Metric Counter with a Fake Clock
Updated
Problem statement
Simulate a metric counter that runs on a fake clock. The clock starts at time 0. A metric’s count is the number of its recorded events in the last windowSeconds seconds.
Process the arrays from left to right. At index i, the operation is:
increment: record one event formetricNames[i]at the current fake time.count: append the active count formetricNames[i]to the result.advance: move the fake clock forward bytimeDeltas[i]seconds. Its metric name is empty.
At time now, an event at time eventTime is active exactly when eventTime > now - windowSeconds. So an event expires when its age becomes exactly windowSeconds. Operations at the same fake time follow input order. Remove a metric’s state after all of its events expire. Return the results of the count operations in their original order.
Function
metricCounts(operations: String[], metricNames: String[], timeDeltas: int[], windowSeconds: int) → int[]Examples
Example 1
operations = ["increment","increment","count","advance","count","increment","count","advance","count","count"]metricNames = ["click","click","click","","click","view","view","","view","click"]timeDeltas = [0,0,0,5,0,0,0,1,0,0]windowSeconds = 5return = [2,0,1,1,0]The two click events are active at time 0. At time 5, both have reached the expiration boundary. The view event recorded at time 5 is still active at time 6.
Example 2
operations = ["increment","advance","increment","increment","count","count","advance","count","count"]metricNames = ["a","","a","b","a","b","","a","b"]timeDeltas = [0,2,0,0,0,0,2,0,0]windowSeconds = 3return = [2,1,1,1]At time 2, metric a has events from times 0 and 2. At time 4, the event from time 0 is expired, while both events recorded at time 2 remain active.
Constraints
1 <= operations.length <= 100000.- The three operation arrays have equal length.
- Each operation is
increment,count, oradvance. - Metric names used by
incrementandcountare nonempty printable ASCII strings of length at most50. - For
incrementandcount,timeDeltas[i] == 0. - For
advance,metricNames[i]is empty and0 <= timeDeltas[i] <= 10^9. 1 <= windowSeconds <= 10^9.- The fake clock’s total time fits in a signed
64-bit integer.
More Snap Inc. interview questions
- Render an Org Chart and Find Skip-Level PairsONSITE INTERVIEW · Seen Nov 2026
- Peak of an Increasing-Then-Decreasing ArrayONSITE INTERVIEW · Seen Oct 2026
- Number of Closed IslandsONSITE INTERVIEW · Seen Oct 2026
- Radar Barrier CrossingONSITE INTERVIEW · Seen Oct 2026
- Sort Characters by FrequencyONSITE INTERVIEW · Seen Sep 2026
- Dictionary Records to an Editable XML TreeONSITE INTERVIEW · Seen Aug 2026
- ID Allocator with Assign and ReleasePHONE SCREEN · Seen Nov 2026
- Basic Calculator IIPHONE SCREEN · Seen Oct 2026
- Stream Cluster Max and MedianPHONE SCREEN · Seen Oct 2026
- LRU Cache with Thread-Safety Follow-UpPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Per-Client Sliding-Window Rate LimiterNike, Inc. · ONSITE INTERVIEW
- Per-Client Sliding-Window Rate LimiterGeneral Motors · OA
- Completions API Rate LimiterCursor · PHONE SCREEN
- Key-Value Store GET and PUT QPSDatabricks · PHONE SCREEN
- Sliding-Window Rate LimiterAmazon · ONSITE INTERVIEW
- Rate Limiter with Global and Per-User LimitsOpenAI · ONSITE INTERVIEW
- Per-Element Hit CounterUber · ONSITE INTERVIEW
- Rate Limiter Sliding Window With Per-Entity LimitsRoblox · PHONE SCREEN · ONSITE INTERVIEW
- Article Vote TrackerRippling · PHONE SCREEN · ONSITE INTERVIEW
- Sliding-Window Rate LimiterStripe · PHONE SCREEN · ONSITE INTERVIEW