EasyPythonJavaScriptTypeScript

Throttle (Simulation)

FunctionsPatterns

Implement solve(events, wait) that simulates leading-edge throttle over
a fixed timeline.

events is a sorted list of [timestampMs, value] pairs representing
invocations of a throttled function. Return all [emitTimeMs, value] pairs
the throttled function would actually fire.

Semantics (leading edge)

  • The first call in each window fires immediately at its own timestamp.
  • Any call within wait ms of the last fired call is suppressed.
  • After the window expires the next call fires.

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

Sample tests

Test #1Single event always fires
Input: [[[0,"x"]],1000]
Output: [[0,"x"]]
Test #2Leading-edge: first fires, burst suppressed, new window fires
Input: [[[0,"a"],[50,"b"],[200,"c"]],100]
Output: [[0,"a"],[200,"c"]]
Test #3Exactly at window boundary → each fires
Input: [[[0,1],[100,2],[200,3]],100]
Output: [[0,1],[100,2],[200,3]]
Test #4Two burst groups separated by a long gap
Input: [[[0,"a"],[10,"b"],[20,"c"],[300,"d"],[310,"e"]],100]
Output: [[0,"a"],[300,"d"]]
Test #5No events → empty result
Input: [[],100]
Output: []