Problem · Array

Logger Rate Limiter

Learn this problem
EasyAmazon Web Services logoAmazon Web ServicesNEW GRADONSITE INTERVIEW

Problem statement

A logger receives a sequence of messages with integer timestamps. It may print the same message at most once in any 10-second interval.

Process the events in the given order. A message at timestamp t is printed when it has never been printed before, or when its most recent printed occurrence was at or before t - 10. A suppressed event does not change that message's most recent printed timestamp.

Return one boolean per event: true when the event is printed and false when it is suppressed. Different message strings are limited independently.

Function

shouldPrintMessages(timestamps: int[], messages: String[]) → boolean[]

Examples

Example 1

timestamps = [1,2,3,8,10,11]messages = ["foo","bar","foo","bar","foo","foo"]return = [true,true,false,false,false,true]

The first foo and bar events print. Their later occurrences before timestamps 11 and 12, respectively, are suppressed. At timestamp 11, exactly 10 seconds have passed since foo last printed, so it prints again.

Example 2

timestamps = [5,5,14,15]messages = ["x","x","x","x"]return = [true,false,false,true]

The first event prints. The event at timestamp 14 is still only 9 seconds after that printed event. The event at timestamp 15 reaches the boundary and prints.

Example 3

timestamps = [0,1,2,9,10]messages = ["a","b","c","b","b"]return = [true,true,true,false,false]

The three distinct messages print independently. Message b last printed at timestamp 1, so its events at timestamps 9 and 10 are both too early.

Constraints

  • 1 ≤ timestamps.length = messages.length ≤ 200000.
  • 0 ≤ timestamps[i] ≤ 10^9.
  • timestamps is nondecreasing.
  • Each message is a non-empty printable ASCII string of length at most 100.

More Amazon Web Services problems

drafts saved locally
public boolean[] shouldPrintMessages(int[] timestamps, String[] messages) {
    // Write your code here.
}
timestamps[1,2,3,8,10,11]
messages["foo","bar","foo","bar","foo","foo"]
expected[true,true,false,false,false,true]
checking account