← Databricks Interview Insights
I started with the hash map part no problem, but the sliding window hit tracking is where I got tangled.
Start by clarifying requirements: is the timestamp provided with each operation or is it the current time? Then propose a design using a hash map for key-value storage and a per-key time-ordered structure (e.g., deque or circular buffer) to track get timestamps within the sliding window. Discuss trade-offs between memory usage, time complexity, and concurrency, and consider optimizations like lazy deletion or bucketing for high-throughput scenarios.
Pro tip: Mention that you would use a monotonic clock or assume timestamps are non-decreasing to simplify the sliding window logic, and highlight that getHits should be O(1) or O(log n) by evicting expired timestamps lazily.
Ask whether timestamps are provided per operation or use system time, whether keys can expire, and expected scale (QPS, memory). Confirm that getHits returns the count of get calls for a key in the last 5 minutes ending at the given timestamp.
Use a hash map to store key-value pairs for put/get/delete. For getHits, maintain a per-key deque (or circular buffer) of timestamps of get calls, or use a bucketed approach (e.g., 1-second buckets) to reduce memory.
On each get, record the timestamp. On getHits, remove timestamps older than (current_timestamp - 300 seconds) from the front of the deque, then return the size. Ensure operations are efficient and handle out-of-order timestamps if necessary.
Discuss time/space complexity: put/get/delete O(1), getHits O(k) where k is number of gets in window (amortized O(1) with lazy eviction). Consider bucketing to bound memory, and concurrency using locks or sharding.
Test with empty keys, multiple gets at same timestamp, timestamps far apart, and high concurrency. Ensure delete removes both value and hit history, and that getHits works even if no gets occurred.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.