Implement a sliding-window hit counter
DatabricksSoftware Engineer · Technical ScreenLast updated
This question evaluates understanding of time-based data structures and algorithms for sliding-window counters, including fixed-size array bucketing, handling timestamp collisions within buckets, refactoring for reuse, and time/space complexity analysis.
Implement a hit counter that supports recordHit(timestamp) and getHits(pastSeconds). Use a fixed-size array to maintain a sliding time window (e.g., last 300 seconds) so each operation is O(
- and memory is bounded. Explain how you handle timestamp collisions within buckets, generalize to arbitrary window sizes, refactor for reusability, and compare trade-offs versus alternatives (queue, deque, hashmap with buckets). Analyze time and space complexity.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.