Your question is Debounce or Throttle Events. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
The Deliveroo search experience can receive many input events while a user is typing. Implement a trailing-edge debounce simulation that keeps only the latest event during each burst and emits it after a period of inactivity.
Implement debounce_events(events, wait), where events is a list of [timestamp, value] pairs sorted by nondecreasing timestamp. Each timestamp is an integer number of milliseconds, and wait is the required quiet interval in milliseconds.
For every event, schedule its value to be emitted at timestamp + wait. If another event arrives before that scheduled time, cancel the previous pending emission and schedule the new event instead. If an event arrives exactly at the scheduled time, emit the previous event first, because the quiet interval has completed. Return all emitted events as [emission_timestamp, value] pairs, including the final pending event.
Do not mutate events. You are simulating debounce behavior, not using timers or browser APIs.
def debounce_events(events, wait):