Design KV store with sliding-window average QPS
Company: Databricks
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to design an in-memory key–value store and compute average QPS over a sliding time window, testing competencies in time-windowed aggregation, handling mutation operations, and maintaining efficient update/query performance.
Constraints
- 0 <= len(operations) <= 2 * 10^5
- Timestamps are non-decreasing integers in the range [0, 10^9]
- 1 <= windowSeconds <= 10^5
- Keys are strings; values can be any Python literal
- DELETE counts as a mutation even if the key is absent
Examples
Input: [("PUT", 10, "a", "1"), ("PUT", 11, "b", "2"), ("DELETE", 13, "a"), ("AVG_QPS", 13, 5)]
Expected Output: [0.6]
Explanation: Mutation timestamps are 10, 11, and 13. In the interval (8, 13], all 3 are included, so the answer is 3 / 5 = 0.6.
Input: [("PUT", 5, "x", 1), ("AVG_QPS", 5, 5), ("DELETE", 5, "y"), ("AVG_QPS", 5, 5)]
Expected Output: [0.2, 0.4]
Explanation: At the first query, only the earlier PUT at timestamp 5 has been processed, so the count is 1. At the second query, both the PUT and DELETE at timestamp 5 have been processed, so the count is 2.
Input: [("PUT", 1, "a", 1), ("PUT", 3, "b", 2), ("DELETE", 6, "a"), ("AVG_QPS", 6, 3), ("AVG_QPS", 6, 5)]
Expected Output: [0.3333333333333333, 0.4]
Explanation: For window 3, the interval is (3, 6], so timestamp 3 is excluded and only timestamp 6 counts: 1 / 3. For window 5, the interval is (1, 6], so timestamps 3 and 6 count: 2 / 5.
Input: [("DELETE", 2, "ghost"), ("PUT", 4, "a", 7), ("DELETE", 10, "a"), ("AVG_QPS", 10, 10), ("AVG_QPS", 10, 6)]
Expected Output: [0.3, 0.16666666666666666]
Explanation: DELETE on a missing key still counts as a mutation. In (0, 10], all three mutations count, so 3 / 10 = 0.3. In (4, 10], timestamp 4 is excluded, so only the DELETE at 10 counts: 1 / 6.
Input: [("AVG_QPS", 7, 4)]
Expected Output: [0.0]
Explanation: No mutation operations have occurred before the query, so the count is 0 and the average QPS is 0.0.
Input: []
Expected Output: []
Explanation: With no operations, there are no AVG_QPS results to return.
Hints
- The QPS calculation depends only on mutation timestamps, not on stored values. Try keeping the timestamps of PUT and DELETE operations in sorted order as you process the list.
- For a query at time t, use binary search to find how many processed mutation timestamps are greater than t - windowSeconds and less than or equal to t.