Interview questions

Implement throttle

A common coding round: write a throttle utility and contrast it with debounce.

intermediate All levels

Throttle guarantees a function runs at most once per time interval, no matter how often it is called — ideal for scroll, mousemove or rapid-fire button clicks.

Throttle is a turnstile that admits one person every few seconds; debounce is a door that only closes once everyone has stopped arriving.

Key concepts

1
The implementation tracks whether we are in a cooldown period: the first call runs immediately, then further calls are ignored until the interval elapses.
cooldown
2
Be ready to contrast it with debounce: throttle fires on a steady cadence during a burst, while debounce waits for the burst to stop before firing once.
debounce