Skip to the code
cf.
+10 XP

#719 When Does the Search Request Fire?

easy

A search box debounces its input handler: each keystroke cancels the pending request and schedules a new one wait milliseconds later. A request is only sent when wait ms pass with no new keystroke.

Write debounceRuns(calls, wait) where calls holds the keystroke times in milliseconds, sorted ascending. Return the times when requests are sent, in order. If a keystroke happens at exactly the time a request is due, the request is sent first and the keystroke then schedules a new one.

Example 1

Input: calls = [0,100,250,600], wait = 200
Output: [450,800]

0, 100, 250 are each less than 200 ms apart, so one request fires at 250 + 200. The keystroke at 600 fires at 800.

Example 2

Input: calls = [0,200], wait = 200
Output: [200,400]

The request due at 200 is sent before the keystroke at 200 schedules the next one.

Constraints

  • 0 <= calls.length <= 10^5
  • 0 <= calls[i] <= 10^9, sorted ascending (equal times allowed).
  • 1 <= wait <= 10^6
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).