EPAM India frontend interfaces often receive rapid input, resize, or interaction events. Implement a trailing-edge debounce simulation that invokes a callback only after no new event has arrived for a specified quiet period.
Given chronologically ordered events, each containing a timestamp and value, return the invocations produced by the debounced handler. An event at time t schedules an invocation at t + wait. If another event arrives before that time, the pending invocation is cancelled and rescheduled using the newer event. The final pending event must be emitted after the input ends.
Implement debounce_events(events, wait). events is a list of two-element lists [timestamp, value], where timestamps are nonnegative integers and values are integers. wait is a nonnegative integer. Return a list of two-element lists [invocation_time, value] for each callback invocation.
If a new event arrives exactly when a pending invocation is scheduled, emit the pending invocation first, then process the new event.
def debounce_events(events, wait):