Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Debounce or Throttle Function

MediumPython00:00
Practice interviewer
In session
5 left
00:00

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.

You need to log in / sign up to run or submit.

Problem

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.

Formal Specification

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.

Constraints

  • 0 <= len(events) <= 10^5
  • Timestamps are integers in nondecreasing order
  • 1 <= delay <= 10^9
  • Each event contains exactly one timestamp and one payload

Function Signature

def debounce_events(events, delay):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output