Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Custom Hook for Debounce/Throttle

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

Your question is Custom Hook for Debounce/Throttle. 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

Tech(x Search receives rapidly changing query events from the UI. Implement a debounce algorithm that emits only the most recent event after no newer event arrives for delay time units.

An incoming event at time t cancels the currently pending event if t is less than or equal to its scheduled emission time. When the next event arrives after the scheduled time, emit the pending event before processing the new one. After processing all input, emit any remaining pending event.

Formal Specification

Implement process_debounced(events, delay), where events is a list of [timestamp, value] pairs sorted by nondecreasing timestamp, and delay is a nonnegative integer. Return a list of [emission_timestamp, value] pairs in emission order. Values are opaque payloads and must be preserved unchanged.

Constraints

  • 0 <= len(events) <= 10^5
  • 0 <= events[i][0] <= 10^9
  • 0 <= delay <= 10^9
  • Events are sorted by timestamp
  • Each event contains exactly one timestamp and one value

Function Signature

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