← Questions

Debounce and throttle in JavaScript

0
AmazonCodingLast asked Sep 2025

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 calling fn until wait milliseconds have passed since the last call.
  • throttle(fn, wait, options) returns a function that calls fn at most once every wait milliseconds.

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 }. maxWait caps 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).
  • fn is called with the this and 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 leading and trailing on and only one call in a window, should fn run once or twice? (Once.)
  • What should the wrapper return: the last result of fn, or nothing?
  • Should timers use Date.now() or performance.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.
  • cancel clearing the timer and stored arguments; flush running the pending call once and clearing it.
  • Re-entrancy (fn calling the wrapper again) and errors thrown by fn reaching 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 requestAnimationFrame for 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

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