Design In-Memory QPS Counter
Company: Databricks
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates the ability to design and implement an in-memory time-series counter and sliding-window aggregation, emphasizing efficient data structures, handling high-frequency events, and memory-time trade-offs.
Part 1: Implement an In-Memory QPS Counter for a 5-Minute Sliding Window
Constraints
- 0 <= len(operations) <= 200000
- 0 <= timestamp, now <= 10^9
- Operation timestamps are globally non-decreasing
- Keys are strings
- The QPS for a query is count_in_window / 300.0
Examples
Input: [('record', 'api', 1), ('record', 'api', 2), ('record', 'api', 300), ('get_qps', 'api', 300), ('get_qps', 'api', 301)]
Expected Output: [0.01, 0.006667]
Explanation: At now=300, window [1,300] has 3 requests. At now=301, window [2,301] has 2 requests.
Input: [('record', 'a', 10), ('record', 'b', 10), ('record', 'a', 11), ('get_qps', 'a', 11), ('get_qps', 'b', 11)]
Expected Output: [0.006667, 0.003333]