477. Filter Duplicate Notifications
Filter Duplicate Notifications

Design an in-memory NotificationFilter to filter duplicate notifications. Each notification has a UUID string called notificationId.

A notification is a duplicate if the same ID arrived less than 600 seconds earlier. Compare against its most recent arrival, including rejected duplicates. Every arrival updates that ID's last-seen time.

Method Signatures

NotificationFilter()
  • Creates an empty filter.
boolean shouldSend(String notificationId, long arrivalTime)
  • notificationId: the notification's ID.
  • arrivalTime: its arrival time in seconds. Use this input instead of the system clock.
  • Checks for a duplicate, records the arrival, and returns false for a duplicate or true otherwise.

Constraints

  • Every ID is a valid, lowercase, 36-character UUID string. Parameters are never null.
  • 0 ≤ arrivalTime ≤ 1,000,000,000,000.
  • Calls are processed sequentially with non-decreasing arrival times. Equal times are allowed; process those calls in their given order.

Performance Requirements

  • Run entirely in memory on one application instance, handling approximately 10,000 notifications per second.
  • Remove expired records during each call. Use O(N) memory, where N is the number of distinct IDs seen within the current 600-second window.
  • Target amortized O(1) or O(log N) processing time per call, including cleanup.

Examples

Example 1: Repeated arrivals extend the window

Create a new filter with NotificationFilter(), then make these calls in order:

  1. shouldSend(notificationId = "748e219a-4be6-4c2c-972b-1a63c89d425f", arrivalTime = 20) returns true: this ID is new.
  2. shouldSend(notificationId = "748e219a-4be6-4c2c-972b-1a63c89d425f", arrivalTime = 300) returns false: only 280 seconds have passed.
  3. shouldSend(notificationId = "748e219a-4be6-4c2c-972b-1a63c89d425f", arrivalTime = 620) returns false: only 320 seconds have passed since the latest arrival.
  4. shouldSend(notificationId = "748e219a-4be6-4c2c-972b-1a63c89d425f", arrivalTime = 1220) returns true: exactly 600 seconds have passed since the latest arrival.

Example 2: Different IDs at the same time

Create a new filter with NotificationFilter(), then make these calls in order:

  1. shouldSend(notificationId = "748e219a-4be6-4c2c-972b-1a63c89d425f", arrivalTime = 50) returns true: this ID is new.
  2. shouldSend(notificationId = "b913d7e4-2c86-42da-a3e1-f67805bc192d", arrivalTime = 50) returns true: this is a different ID.
  3. shouldSend(notificationId = "748e219a-4be6-4c2c-972b-1a63c89d425f", arrivalTime = 50) returns false: the same ID already arrived at this time.


Please use Laptop/Desktop or any other large screen to add/edit code.