Skip to content
RED SAGSkills
medium30 ptsfrontendsimulation

Simulate a Debounced Search Box

A search box debounces its input: every keystroke restarts a waitMs timer, and the search only runs when the timer completes without another keystroke. It runs with the value of the most recent keystroke.

events is a list of { t, value } keystrokes sorted by time t (ms). If a keystroke arrives at exactly the moment the timer would fire, the timer fires first.

Return the searches that run, as { time, value } objects in time order.

Example 1

Input: events = [{"t":0,"value":"a"},{"t":100,"value":"ab"},{"t":250,"value":"abc"},{"t":1000,"value":"x"}], waitMs = 200
Output: [{"time":450,"value":"abc"},{"time":1200,"value":"x"}]
Explanation: Typing pauses after "abc" for long enough, and again after "x".

Example 2

Input: events = [{"t":0,"value":"a"},{"t":200,"value":"b"}], waitMs = 200
Output: [{"time":200,"value":"a"},{"time":400,"value":"b"}]

Constraints

  • 0 <= events.length <= 10^5
  • 1 <= waitMs <= 10^4
  • times are non-decreasing integers
Hint 1

A keystroke triggers a search exactly when the next keystroke is at least waitMs later, or there is no next keystroke.

Discussion 0

Join free to post in the discussion.

Good practice for: Frontend Developer, Full Stack Developer, Mobile App Developer, Automation Test Engineer

Run checks your code on 3 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.