Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Debounce or Throttle Utility

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

Your question is Debounce or Throttle Utility. 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

Dream11 search suggestions should update only after the user stops typing for a specified interval. Implement a trailing-edge debounce utility that suppresses intermediate events and emits only the latest event after no newer event arrives within wait time units.

Formal Specification

Write debounce_events(events, wait), where events is a list of events represented as [timestamp, value]. Timestamps are non-decreasing integers, and values may be strings or other JSON-compatible values. Return a new list of [emit_timestamp, value] pairs.

The first event in a burst is replaced whenever another event arrives within wait time units. The latest event is emitted at event_timestamp + wait. If a new event arrives at exactly the pending event's deadline, emit the pending event first, then begin a new burst with the current event. The input list is already complete, so flush the final pending event before returning.

Constraints

  • 0 <= len(events) <= 10^5
  • wait >= 1
  • Timestamps are non-decreasing integers
  • Each event contains exactly one timestamp and one value

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