← Questions
Debounce and throttle in JavaScript
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's technical phone screen, last asked Sep 2025. It tests javascript, frontend and timers.
Implement debounce and throttle in JavaScript, as a utility library would ship them.
-
debounce(fn, wait, options)returns a function that delays callingfnuntilwaitmilliseconds have passed since the last call. -
throttle(fn, wait, options)returns a function that callsfnat most once everywaitmilliseconds.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
options = { leading: boolean, trailing: boolean, maxWait?: number }— debounce defaults to{ leading: false, trailing: true }; throttle defaults to{ leading: true, trailing: true }.maxWaitcaps how long a debounced call can be put off. - The returned function has
.cancel()(drop any pending call) and.flush()(run a pending call now and return its result). -
fnis called with thethisand arguments of the latest call.
Example
const d = debounce(log, 100)
d("a") at 0ms, d("b") at 50ms, d("c") at 120ms
-> log("c") runs once, at 220ms
const t = throttle(log, 100)
t("a") at 0ms, t("b") at 30ms, t("c") at 60ms
-> log("a") at 0ms (leading), log("c") at 100ms (trailing, latest arguments)
const d2 = debounce(log, 100, { leading: true, trailing: false })
d2("a") at 0ms, d2("b") at 50ms, d2("c") at 300ms
-> log("a") at 0ms, log("c") at 300ms
Clarifying questions
- With both
leadingandtrailingon and only one call in a window, shouldfnrun once or twice? (Once.) - What should the wrapper return: the last result of
fn, or nothing? - Should timers use
Date.now()orperformance.now()?
What a strong answer covers
- State kept in the closure: the timer id, the last arguments and
this, the time of the last call and of the last invocation, and the last result. - Computing remaining time from timestamps rather than trusting that timers fire exactly on time (timer drift, a sleeping tab).
- Throttle built as debounce with
maxWait = wait, sharing one implementation. -
cancelclearing the timer and stored arguments;flushrunning the pending call once and clearing it. - Re-entrancy (
fncalling the wrapper again) and errors thrown byfnreaching the caller without leaving the wrapper stuck. - Tests with fake timers: bursts, a single call, leading and trailing combinations, cancel, flush,
maxWait.
Follow-ups
- Make the debounced function return a promise that resolves with
fn's result. - Debounce per key, for example one timer per search field.
- Throttle with
requestAnimationFramefor scroll handlers.
Where it's asked
- Amazon · Technical phone screen · Last asked Sep 202560 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI
What it tests
javascript · frontend · timers
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
