FastPrepOut-of-Order Logger Rate Limiter

Out-of-Order Logger Rate Limiter

Google logoGoogle● HardFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW

Problem statement

A logger receives messages whose integer event timestamps may arrive out of order. Process the paired arrays timestamps and messages in input order.

For an event with timestamp t and message m, print it exactly when no earlier accepted event for m has an event timestamp in the inclusive interval [t - 9, t]. An earlier-processed event whose timestamp is greater than t does not block this event. A rejected event does not update the accepted history, and decisions are never revised retroactively.

Return one boolean per event: true when it prints and false when it is suppressed. Equal-timestamp events retain input order, and different messages are limited independently.

Function

shouldPrintOutOfOrder(timestamps: long[], messages: String[]) → boolean[]

Examples

Example 1

timestamps = [10,15,5,20]messages = ["foo","foo","foo","foo"]return = [true,false,true,true]

The event at 10 prints, so the one at 15 is suppressed. Timestamp 5 then prints because the already processed timestamp 10 is later than its query interval. Timestamp 20 is ten seconds after the accepted event at 10, so it also prints.

Example 2

timestamps = [20,11,12,21,30,22]messages = ["a","a","a","a","a","b"]return = [true,true,false,false,true,true]

After timestamp 20 prints, the out-of-order event at 11 also prints. Timestamp 12 is blocked by 11, and timestamp 21 is blocked by 20. Timestamp 30 reaches the ten-second boundary, while message b has independent state.

Constraints

  • 1 <= timestamps.length == messages.length <= 100000.
  • 0 <= timestamps[i] <= 1000000000000; timestamps need not be sorted.
  • Each message contains from 1 through 50 lowercase English letters or digits.
  • Use signed 64-bit arithmetic for timestamps and interval boundaries.

More Google problems

See Google hiring insights
Saved
public boolean[] shouldPrintOutOfOrder(long[] timestamps, String[] messages) {
    // Write your solution here.
}
timestamps[10,15,5,20]
messages["foo","foo","foo","foo"]
expected[true,false,true,true]
Checking account…