Your question is Debounce or Throttle Function. 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.
Flipkart search suggestions should be requested only after the user pauses typing. Implement a trailing-edge debounce simulation that emits the final event in each burst of activity after a specified period of inactivity.
Write debounce_events(events, delay), where events is a list of events in nondecreasing timestamp order. Each event is represented as [timestamp, payload], where timestamp is an integer and payload is any JSON-compatible value. delay is a positive integer.
Return a list of emitted events represented as [fire_timestamp, payload]. For every consecutive burst, keep only its final event. If the final event in a burst occurs at time t, emit it at time t + delay. A new event starts a separate burst when its timestamp is at least delay units after the previous event, because the previous callback would already have fired.
Do not use real timers, threads, or asynchronous APIs. The function must produce a deterministic simulation from the input events.
def debounce_events(events, delay):