Message Rate Limiter

Quick Overview

Implement per-message rate-limit decisions from chronological timestamps, where only accepted occurrences reset each message's window.

Message Rate Limiter

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Message Rate Limiter Implement `message_rate_limit_decisions(timestamps: list[int], messages: list[str], window_seconds: int) -> list[bool]`. Process a chronological stream of timestamped messages. A message is accepted if the same message text has not been accepted during the preceding rate-limit window. Return whether each event is accepted. ### Input Domain - `0 <= len(timestamps) = len(messages) <= 200,000`. - Timestamps are nonnegative signed 64-bit integers in nondecreasing order. - Message strings are nonempty ASCII text. - `1 <= window_seconds <= 10^9`. ### Output Rules - At timestamp `t`, a message is accepted when it has no prior accepted occurrence or `t - last_accepted >= window_seconds`. - A rejected event does not update the message's last accepted timestamp. - Different message strings are limited independently. - Preserve event order and return an empty list for empty input. ### Constraints - Decisions for equal timestamps are processed in input order. - Target average time is `O(1)` per event. ### Examples #### Example 1 Input: `timestamps = [1,2,3,11], messages = ["foo","bar","foo","foo"], window_seconds = 10` Output: `[true,true,false,true]` #### Example 2 Input: `timestamps = [1,5,9,10], messages = ["x","x","x","x"], window_seconds = 5` Output: `[true,false,true,false]` ```hint Remember only accepted occurrences For each distinct message, its most recent accepted timestamp is sufficient to decide the next event. ```

Overview: Implement per-message rate-limit decisions from chronological timestamps, where only accepted occurrences reset each message's window.

|Home/Coding & Algorithms/Amazon
Amazon logo
Amazon
Aug 20, 2026
mediumSoftware EngineerOnsiteCoding & Algorithms
3
0

Message Rate Limiter

Implement message_rate_limit_decisions(timestamps: list[int], messages: list[str], window_seconds: int) -> list[bool].

Process a chronological stream of timestamped messages. A message is accepted if the same message text has not been accepted during the preceding rate-limit window. Return whether each event is accepted.

Input Domain

  • 0 <= len(timestamps) = len(messages) <= 200,000 .
  • Timestamps are nonnegative signed 64-bit integers in nondecreasing order.
  • Message strings are nonempty ASCII text.
  • 1 <= window_seconds <= 10^9 .

Output Rules

  • At timestamp t , a message is accepted when it has no prior accepted occurrence or t - last_accepted >= window_seconds .
  • A rejected event does not update the message's last accepted timestamp.
  • Different message strings are limited independently.
  • Preserve event order and return an empty list for empty input.

Constraints

  • Decisions for equal timestamps are processed in input order.
  • Target average time is O(1) per event.

Examples

Example 1

Input: timestamps = [1,2,3,11], messages = ["foo","bar","foo","foo"], window_seconds = 10

Output: [true,true,false,true]

Example 2

Input: timestamps = [1,5,9,10], messages = ["x","x","x","x"], window_seconds = 5

Output: [true,false,true,false]

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...