Implement a Log Rate Limiter
Company: Roblox
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates a candidate's ability to design and implement time-based rate limiting and stateful event tracking using appropriate data structures and algorithmic analysis.
Constraints
- Timestamps arrive in nondecreasing order.
- 0 <= timestamp <= 10^9
- 1 <= number of requests <= 10^5
- Each message is a non-empty string.
- The cooldown window is exactly 10 seconds: re-emit allowed when timestamp - lastEmitted >= 10.
Examples
Input: ([[1, "foo"], [2, "bar"], [3, "foo"], [11, "foo"]],)
Expected Output: [True, True, False, True]
Explanation: "foo" at t=1 and "bar" at t=2 are firsts (emit). "foo" at t=3 is only 2s later (suppress). "foo" at t=11 is 10s after t=1 (emit).
Input: ([],)
Expected Output: []
Explanation: No requests -> empty result list.
Input: ([[1, "a"]],)
Expected Output: [True]
Explanation: First and only occurrence of "a" is always emitted.
Input: ([[1, "x"], [10, "x"], [11, "x"], [21, "x"]],)
Expected Output: [True, False, True, True]
Explanation: x@1 emits. x@10 is 9s later (suppress). x@11 is 10s after t=1 (emit, window resets to 11). x@21 is 10s after t=11 (emit).
Input: ([[1, "foo"], [1, "foo"], [1, "bar"]],)
Expected Output: [True, False, True]
Explanation: Duplicate "foo" at the same timestamp: second is suppressed (0s gap). "bar" is a different key, so it emits.
Input: ([[0, "m"], [9, "m"], [10, "m"]],)
Expected Output: [True, False, True]
Explanation: Boundary check: m@9 is only 9s after m@0 (suppress); m@10 is exactly 10s after m@0 (emit).
Hints
- Track, for each distinct message, the last timestamp at which it was emitted.
- A message is allowed when it has never been seen, or when current_ts - last_ts >= 10.
- Only update the stored timestamp on the requests you actually emit — a suppressed request does not reset the 10-second window.
- A hash map keyed by message gives O(1) amortized work per request.