Implement a Rolling Hit Counter
Company: Databricks
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
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
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
- Trace two queries at the same timestamp with a hit recorded between them.
Community answers
Answer by mirapt06