Implement Real-Time Rate Limiting for Web Service Requests
Company: Atlassian
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates understanding of real-time rate limiting, time-windowed traffic control, and efficient per-client state management under algorithmic constraints.
Constraints
- 0 <= n <= 200000 where n = len(addresses)
- addresses[i] is a non-empty string (1 to 100 characters)
- Request i occurs at time t = i seconds (0-indexed)
- For each address independently: at most 2 accepted in any inclusive 5-second window [t-4, t]
- For each address independently: at most 5 accepted in any inclusive 30-second window [t-29, t]
- Return a list of "200" or "429" of length n
Hints
- Maintain per-address sliding windows using queues/deques of accepted timestamps.
- For each incoming request at time t, evict timestamps < t-4 from a 5-second deque and < t-29 from a 30-second deque.
- Accept if both deques have sizes strictly below their limits before adding t; otherwise reject.
- Use a hash map from address to its deques for O(1) amortized updates.