Problem, as an interviewer would state it:
"Implement throttle(fn, wait, options) — guarantee fn runs at most once every wait ms, even under continuous, rapid calls, with configurable leading/trailing edges."
Examples:
const t = throttle(onScroll, 100);
window.addEventListener("scroll", t);
// fires at most once per 100ms while scrolling continuously, not once per pixel
Clarifying questions expected:
- Does the FINAL call of a continuous burst need to be captured (trailing edge), or is it acceptable to drop it?
- Should the first call fire immediately (leading edge), or only after the first full window?
- Is
waita fixed window from the FIRST call, or does it reset on every subsequent call (which would make this debounce, not throttle)?
Code / implementation expected: Yes — real, direct proof via a timed burst that both the leading and trailing edges fire correctly and calls are correctly rate-limited.