Implement a rate-limited hit counter
Company: Databricks
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates a candidate's understanding of designing efficient time-windowed data structures and algorithmic analysis, including space-time trade-offs and amortized complexity.
Constraints
- 0 <= len(operations) <= 10^5
- len(operations) == len(timestamps)
- operations[i] is either "hit" or "getHits"
- 1 <= timestamps[i] <= 10^9 when the input is non-empty
- timestamps is in non-decreasing order
Examples
Input: (["hit", "hit", "hit", "getHits", "hit", "getHits", "getHits"], [1, 2, 3, 4, 300, 300, 301])
Expected Output: [3, 4, 3]
Explanation: At time 4, the three earlier hits are counted. At time 300, hits at 1, 2, 3, and 300 are all within [1, 300]. At time 301, the hit at 1 expires, so only hits at 2, 3, and 300 remain.
Input: (["hit", "hit", "hit", "getHits", "hit", "getHits"], [1, 1, 1, 1, 300, 300])