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

Debounce or Throttle Mechanism

MediumPython

Problem

Shopee search suggestions should update only after the user has stopped typing for a specified quiet period. Given a chronological stream of input events, implement trailing-edge debouncing and return the events that would trigger a search request.

An event is represented as [timestamp, value], where timestamp is an integer time unit and value is a string. When a new event arrives before the current candidate has remained unchanged for delay time units, replace the candidate with the new event. A candidate triggers at candidate_timestamp + delay when the next event occurs at least delay units later. After processing all events, emit the final candidate at its scheduled trigger time.

Formal Specification

Implement debounce_events(events, delay). The input events is a list of timestamp-value pairs sorted by nondecreasing timestamp. Return a list of [trigger_timestamp, value] pairs in chronological processing order. The input list may be empty.

Constraints

  • 0 <= len(events) <= 10^5
  • 0 <= events[i][0] <= 10^9
  • 1 <= delay <= 10^9
  • events is sorted by nondecreasing timestamp
  • Each event value is a nonempty string

Function Signature

def debounce_events(events, delay):
Interviewer

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