Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Debounce or Throttle From Scratch
00:00
5 left

Debounce or Throttle From Scratch

MediumPython

Problem

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.

Formal Specification

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.

Constraints

  • 0 <= events.length <= 10^5
  • Events are sorted by nondecreasing timestamp.
  • 0 <= timestamp <= 10^9
  • 0 <= wait <= 10^9
  • Values are integers.

Function Signature

def debounce_events(events, wait):
Interviewer

Your question is Debounce or Throttle From Scratch. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.