FastPrepMetric Counter with a Fake Clock
Problems/Metric Counter with a Fake Clock

Metric Counter with a Fake Clock

Snap Inc. logoSnap Inc.● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Snap Inc. onsite interview. The report describes a metric counter, unit tests, and a fake timer. It does not say how entries age over time, what the operations are, where expiry cuts off, or what a query returns. Our version is about 89% the same as the reported question.

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 for metricNames[i] at the current fake time.
  • count: append the active count for metricNames[i] to the result.
  • advance: move the fake clock forward by timeDeltas[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, or advance.
  • Metric names used by increment and count are nonempty printable ASCII strings of length at most 50.
  • For increment and count, timeDeltas[i] == 0.
  • For advance, metricNames[i] is empty and 0 <= 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

Interview reports that mention this question

  • Snap AI Coding Interview, Experienced SWE and MLE: 4 Experiences

Snap Inc. interview experiences

  • Snap Experienced MLE Onsite Interview Questions: 1 Experience
  • Snap Experienced MLE Virtual Onsite: 1 Experience
See Snap Inc. hiring insights
Saved
class Solution {
    public int[] metricCounts(String[] operations, String[] metricNames, int[] timeDeltas, int windowSeconds) {
        // Write your code here.
    }
}
Checking account…