Facebook Pixel

Debounce Interview Questions (Flipkart)

Flipkart and other companies ask debounce. Here are the questions and answers.

Debounce Interview Questions (Flipkart)

Q1: Implement debounce from scratch.

function debounce(fn, delay) { let timer; return function (...args) { clearTimeout(timer); timer = setTimeout(() => fn.apply(this, args), delay); }; }

Q2: How does debounce work internally?

Each call clears the previous timer and sets a new one. Only the last call within the delay period executes. The timer variable is kept alive by a closure.

Q3: What is the difference between debounce and throttle?

Debounce: delays until activity stops (search input). Throttle: limits to once per interval (scroll handler).

Q4: How do you test debounce?

Call the debounced function rapidly. Verify that only the last call executes after the delay. Use jest.useFakeTimers() in unit tests.

Q5: How do you add a cancel method?

debounced.cancel = () => clearTimeout(timer);

The Takeaway

Flipkart debounce questions: implement from scratch (closure + clearTimeout + setTimeout), explain how it works (last call wins), difference from throttle, how to test (fake timers), and cancel method.

function debounce(fn, delay) { let timer; return function(...args) { clearTimeout(timer); timer = setTimeout(() => fn.apply(this, args), delay); }; }

Debounce delays execution until activity stops (search input, only the last call runs). Throttle limits to once per interval (scroll handler, calls at most once per N ms).

Use jest.useFakeTimers() to control time. Call the debounced function multiple times, advance the timer by the delay, and verify only the last call's callback executed.

Attach a cancel property: debounced.cancel = () => clearTimeout(timer). Call debounced.cancel() to cancel the pending execution.

300ms is the standard. It is fast enough to feel responsive and slow enough to skip intermediate keystrokes. The interviewer may ask you to justify the choice.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.