EasyPythonJavaScriptTypeScript

Debounce (Simulation)

FunctionsAsyncPatterns

Real debounce is timing-based and hard to test deterministically. Instead,
implement solve(events, wait) that simulates trailing-edge debounce
over a fixed timeline.

events is a list of [timestampMs, value] pairs representing calls to a
debounced function. Return a list of [emitTimeMs, value] pairs that the
debounced function would actually fire.

Semantics (trailing edge)

  • Every call resets the timer.
  • The timer fires wait ms after the last call inside a quiet window.
  • If a new call arrives after the previous schedule fired, the previous

emission stays in the result.

Example
solve([[0,'a'], [50,'b'], [200,'c']], 100)[[150,'b'], [300,'c']]

Sample tests

Test #1Single call fires once at t + wait
Input: [[[0,"a"]],100]
Output: [[100,"a"]]
Test #2Burst inside window keeps only the last value
Input: [[[0,"a"],[50,"b"]],100]
Output: [[150,"b"]]
Test #3Two separate quiet windows
Input: [[[0,"a"],[50,"b"],[200,"c"]],100]
Output: [[150,"b"],[300,"c"]]
Test #4No events → no emissions
Input: [[],100]
Output: []
Test #5Each call is fully outside the previous window
Input: [[[0,1],[200,2],[400,3]],100]
Output: [[100,1],[300,2],[500,3]]