Amazon questions

Message Rate Limiter

AmazonPhone screenEasy

A logging service receives timestamped messages and suppresses repeats. Message i carries the text messages[i] and arrives at time timestamps[i]. The timestamps never decrease, and messages are processed in the order given.

Part 1: Accept or reject each message

Each message is either accepted or rejected. A message with text s arriving at time t is accepted when no earlier message with text s was accepted, or when t - last >= window_seconds, where last is the arrival time of the most recently accepted message with text s. Otherwise it is rejected.

A rejected message is dropped: it does not change last. Texts with different content never affect each other, and comparison is case-sensitive.

Return one boolean per message, in arrival order: True for an accepted message, False for a rejected one.

  1. Example 1
    messages
    ["disk full","disk full","disk full","disk full","cpu hot","cpu hot","disk full","disk full","cpu hot"]
    timestamps
    [1,3,11,12,12,15,21,21,22]
    window_seconds
    10
    Output
    [true,false,true,false,true,false,true,false,true]

    Why: With a window of 10: 'disk full' is accepted at 1, rejected at 3, accepted at 11 (exactly 10 after the last accepted copy) and rejected at 12. 'cpu hot' has its own timer: accepted at 12, rejected at 15. At 21 'disk full' is accepted (10 after 11) and its copy at the same time is rejected; 'cpu hot' at 22 is accepted (10 after 12).

Constraints

  • len(timestamps) == len(messages)
  • 0 <= len(messages) <= 2 * 10^5
  • 0 <= timestamps[i] <= 10^18
  • timestamps[i] <= timestamps[i + 1]
  • 1 <= window_seconds <= 10^9
  • 1 <= len(messages[i]) <= 50
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question