Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Debounce Function Implementation

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

Your question is Debounce Function Implementation. 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

Similarweb search and filter surfaces should avoid processing every keystroke. Implement a debounce simulation that executes only the most recent event after no newer event has arrived for wait time units.

Each event is represented as [timestamp, value], where timestamps are strictly increasing. When an event arrives, it cancels the previously scheduled execution and schedules the new value for timestamp + wait. If the next event arrives at or after the scheduled time, execute the pending event first. After all input events are processed, execute any remaining pending event.

Formal Specification

Implement debounce_events(events, wait).

  • Input: events, a list of [int, str] pairs sorted by strictly increasing timestamp, and wait, a positive integer.
  • Output: a list of [execution_timestamp, value] pairs in execution order.
  • The value executed for a burst is the final event before the quiet period.

Constraints

  • 0 <= len(events) <= 10^5
  • 0 <= events[i][0] <= 10^9
  • Event timestamps are strictly increasing
  • 1 <= wait <= 10^9
  • Each event value is a string

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