Implement debounce(fn, delay)
One of the most common practical interview questions — and one you'll actually reach for in real UI code (search-as-you-type, resize handlers).
Write a function debounce(fn, delay) that returns a new function. Calling the returned function repeatedly should only invoke fn once, after delay milliseconds have passed since the last call — every call before that resets the timer.
const search = debounce((query) => console.log("searching:", query), 300);
search("h");
search("he");
search("hel"); // only THIS call actually fires, 300ms after it happensRequirements:
- The returned function should forward its arguments to
fn. - Calling it again before
delayhas elapsed should reset the wait — not queue a second call.