Quick Overview

Implement a rolling hit counter with timestamp coalescing, precise expiry boundaries, a running total, and amortized constant-time ordered operations.

Implement a Rolling Hit Counter

Company: Databricks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

Implement a hit counter with `hit(timestamp)` and `getHits(timestamp)` that returns the number of hits in a recent window of configurable positive duration W. Several hits may share one timestamp. ### Constraints & Assumptions The source names the hit-counter problem but does not state its window length. Use nondecreasing timestamps across calls and the interval `(timestamp-W, timestamp]` as explicit practice assumptions. ### Clarifying Questions to Ask - Are timestamps globally nondecreasing across writes and queries? - Is a hit exactly W time units old excluded? ```hint Compress repeated timestamps Many hits at the same instant can share one timestamp entry with a count. ``` ### What a Strong Answer Covers - Expiry before queries and a running total. - Coalescing same-timestamp hits and avoiding stale-count errors. - Amortized operation cost and retained-state memory. ### Follow-up Questions - How would fixed-resolution ring buckets bound storage? - What changes when timestamps arrive out of order?

Overview: Implement a rolling hit counter with timestamp coalescing, precise expiry boundaries, a running total, and amortized constant-time ordered operations.

Read the full Databricks Software Engineer interview experience this question came from

Implement a hit counter with configurable positive duration `window`. The counter starts empty. Process `operations` in input order. - `[0, timestamp]` records a hit at `timestamp`. - `[1, timestamp]` queries the number of previously recorded hits in `(timestamp - window, timestamp]`. A hit exactly `window` time units old has expired. Several hits may share a timestamp, and each counts separately. Timestamps are nondecreasing. Calls with the same timestamp execute in input order, so a query cannot count a hit recorded later in the trace. Return the query results in order.

Constraints

  • window is an integer from 1 to 2^53 - 1, inclusive.
  • operations contains 0 through 2^31 - 1 records, each exactly [0, timestamp] or [1, timestamp].
  • Timestamps are nondecreasing integers from -(2^53 - 1) to 2^53 - 1, inclusive.

Examples

Input: (5, [[0, 1], [0, 1], [1, 1], [0, 3], [1, 5], [1, 6], [1, 8]])

Expected Output: [2, 3, 1, 0]

Explanation: Both hits at time 1 are counted at times 1 and 5. They expire at time 6, and the hit at time 3 expires at time 8.

Input: (3, [[1, 10], [0, 10], [1, 10], [0, 10], [1, 10], [1, 12], [1, 13]])

Expected Output: [0, 1, 2, 2, 0]

Explanation: Each query at time 10 sees only the hits already recorded. Both hits have expired at time 13.

Hints

  1. Trace two queries at the same timestamp with a hit recorded between them.

Community answers

Answer by mirapt06

from bisect import bisect_left, bisect_right def simulate_hit_counter(window, operations): hits = [] # timestamps are in non-desceding order res = [] for type, timestamp in operations: if type == 0: hits.append(timestamp) continue left = bisect_left(hits, timestamp - window +1) right = bisect_right(hits, timestamp) res.append(right - left) return res

Implement a Rolling Hit Counter

hardDatabricksSoftware Engineer

Implement a hit counter with configurable positive duration window. The counter starts empty. Process operations in input order.

  • [0, timestamp] records a hit at timestamp.
  • [1, timestamp] queries the number of previously recorded hits in (timestamp - window, timestamp]. A hit exactly window time units old has expired.

Several hits may share a timestamp, and each counts separately. Timestamps are nondecreasing. Calls with the same timestamp execute in input order, so a query cannot count a hit recorded later in the trace. Return the query results in order.

Examples

Example 1
Input
(5, [[0, 1], [0, 1], [1, 1], [0, 3], [1, 5], [1, 6], [1, 8]])
Output
[2, 3, 1, 0]
Notes
Both hits at time 1 are counted at times 1 and 5. They expire at time 6, and the hit at time 3 expires at time 8.
Example 2
Input
(3, [[1, 10], [0, 10], [1, 10], [0, 10], [1, 10], [1, 12], [1, 13]])
Output
[0, 1, 2, 2, 0]
Notes
Each query at time 10 sees only the hits already recorded. Both hits have expired at time 13.

Constraints

  • •window is an integer from 1 to 2^53 - 1, inclusive.
  • •operations contains 0 through 2^31 - 1 records, each exactly [0, timestamp] or [1, timestamp].
  • •Timestamps are nondecreasing integers from -(2^53 - 1) to 2^53 - 1, inclusive.

Comments (1)

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def simulate_hit_counter(window: int, operations: list[list[int]]) -> list[int]:
Sample call
simulate_hit_counter(5, [[0, 1], [0, 1], [1, 1], [0, 3], [1, 5], [1, 6], [1, 8]])
Language
Case 1
Input
(5, [[0, 1], [0, 1], [1, 1], [0, 3], [1, 5], [1, 6], [1, 8]])
Expected
[2, 3, 1, 0]
Case 2
Input
(3, [[1, 10], [0, 10], [1, 10], [0, 10], [1, 10], [1, 12], [1, 13]])
Expected
[0, 1, 2, 2, 0]

Browse More Questions

Show the approach

Approach

Use a FIFO queue of timestamp groups. Each group contains a timestamp and its hit count. Keep the sum of those counts. Before processing a call, remove timestamp groups whose age is at least window and subtract their counts. Since calls arrive in chronological order, an expired group cannot count in a later query.

When recording a hit, increment the count of the last group if it has the current timestamp. Otherwise, append a new group. Increment the running total either way. For a query, return the total. After expiration and any recording for the current call, the queue contains exactly the recorded hits in the current window. This invariant also accounts for several calls at one timestamp because every query sees precisely the earlier calls in input order.

The difference of two permitted timestamps fits in 64 bits. For the JavaScript comparison, any elapsed difference no larger than the maximum window is exact. Any larger positive difference still compares greater than the maximum window.

Time complexity:
O(n) total and O(1) amortized per call for n calls; each group enters and leaves the queue at most once.
Space complexity:
O(g) auxiliary space for the peak number of retained timestamp groups, plus O(q) for q query results.