Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Debounce or Throttle Events

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

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.

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

Problem

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.

Formal Specification

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.

Constraints

  • 0 <= events.length <= 10^5
  • 0 <= events[i][0] <= 10^9
  • Timestamps are sorted in nondecreasing order
  • 1 <= wait <= 10^9
  • Each event value is a string, integer, or boolean

Function Signature

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