Implement Sliding-Window Rate Limiter
Company: Roblox
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of time-based sliding-window rate limiting, per-user quota enforcement, efficient in-memory data structures for time-series event eviction, and optional duplicate-detection using request IDs.
Part 1: Implement Sliding-Window Rate Limiter
Constraints
- 1 <= limit <= 10^5
- 1 <= window_seconds <= 10^9
- 0 <= len(requests) <= 2 * 10^5
- timestamps are integers in nondecreasing order
- user_id is a string
- Rate limits are tracked independently for each user
Examples
Input: (2, 5, [('u1', 1), ('u1', 2), ('u1', 3), ('u1', 6)])
Expected Output: [True, True, False, True]
Explanation: At time 3, user u1 already has two accepted requests in [ -1, 3 ], so the third is rejected. At time 6, the request at time 1 has expired from the window [2, 6].
Input: (1, 3, [('a', 1), ('b', 1), ('a', 2), ('b', 4), ('a', 4)])
Expected Output: [True, True, False, True, True]