Debounce and Throttle: How to Answer the Classic Frontend Coding Interview Question
6 views
Few frontend interview questions show up as often as "implement debounce" or its sibling "implement throttle." They look small, but interviewers use them to check whether you understand closures, timers, the `this` binding, and how real user interfaces behave under load. Here is a practical way to prepare and to answer them with confidence.
What each one actually does
Debounce delays a function until a burst of calls has stopped. If a user types into a search box, a debounced handler with a 300 ms wait only fires once they pause typing for 300 ms. Every new keystroke resets the timer.
Throttle guarantees a function runs at most once per interval. A throttled scroll handler with a 200 ms interval runs regularly while the user scrolls, but never more than five times per second.
A good opening line in the interview is a one-sentence contrast: "Debounce waits for quiet; throttle sets a maximum rate." Then give one product example for each, such as search suggestions for debounce and infinite scroll or resize tracking for throttle.
A clean debounce implementation
function debounce(fn, wait) {
let timer = null;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, args), wait);
};
}
Talk through the three details the interviewer is listening for: the closure keeps `timer` private between calls, `clearTimeout` resets the countdown on every call, and `fn.apply(this, args)` preserves both the caller's context and the latest arguments. Using an arrow function inside setTimeout is what lets `this` refer to the original caller.
A simple throttle implementation
function throttle(fn, interval) {
let last = 0;
return function (...args) {
const now = Date.now();
if (now - last >= interval) {
last = now;
fn.apply(this, args);
}
};
}
This is the timestamp, or "leading edge," version. Mention that it drops the final call in a burst, and offer to add a trailing call with a timer if the product needs the last event, for example to save the final scroll position.
Follow-up questions to expect
Leading versus trailing: can debounce fire immediately on the first call and then ignore the rest? Add an `immediate` flag and explain when it helps, such as preventing double-clicks on a submit button.
Cancel and flush: real libraries expose `cancel()` to clear a pending call and `flush()` to run it right away. Adding these shows you have thought about component unmount and cleanup.
React usage: a debounced function recreated on every render never fires correctly. Mention wrapping it in `useMemo` or `useRef` and cancelling it in a cleanup function.
Testing: describe using fake timers in Jest to advance time and assert how many times the callback ran.
How to practise
Write both functions from memory three or four times, then explain them out loud as if to an interviewer. Time yourself: most interviewers expect a working debounce in under ten minutes, leaving time for follow-ups. Finally, build a tiny demo with a search input and a scroll counter so you can describe the behaviour from experience rather than theory.
Even well-prepared candidates sometimes blank when a follow-up comes out of nowhere. Linkjob AI (https://linkjob.ai) offers real-time help during job interviews and technical interviews, so you can stay calm, structure your explanation, and keep the conversation moving.