Debounce - Problem

Imagine you're developing a search feature for an e-commerce website. Every time a user types a character, your code wants to make an API call to fetch search results. But if a user types quickly, you'd end up making dozens of unnecessary API calls!

Debouncing is a technique that solves this problem by delaying function execution and canceling previous calls if new ones arrive within a time window.

Given a function fn and a time delay t milliseconds, return a debounced version of that function. The debounced function should:

  • Delay execution by t milliseconds
  • Cancel previous executions if called again within the delay window
  • Pass through all parameters to the original function

Example: If t = 50ms and calls happen at 30ms, 60ms, and 100ms:

  • Call at 30ms → Scheduled for 80ms, but gets cancelled
  • Call at 60ms → Scheduled for 110ms, but gets cancelled
  • Call at 100ms → Finally executes at 150ms

Input & Output

example_1.js — Basic Debounce
$ Input: fn = (x) => x * 2, t = 100ms Calls: debounced(5) at 0ms, debounced(10) at 50ms
Output: Function executes once with argument 10 at 150ms, returns 20
💡 Note: The first call gets cancelled when the second call arrives within the 100ms window. Only the second call executes after the full delay.
example_2.js — Multiple Parameters
$ Input: fn = (a, b) => a + b, t = 50ms Calls: debounced(1, 2) at 0ms, debounced(3, 4) at 25ms, debounced(5, 6) at 75ms
Output: Function executes once with arguments (5, 6) at 125ms, returns 11
💡 Note: All parameters are preserved through the debounce mechanism. The final call wins and executes with its original arguments.
example_3.js — Spaced Calls
$ Input: fn = (msg) => console.log(msg), t = 100ms Calls: debounced('A') at 0ms, debounced('B') at 150ms
Output: Logs 'A' at 100ms, then logs 'B' at 250ms
💡 Note: Since the calls are spaced more than 100ms apart, both execute independently without cancellation.

Visualization

Tap to expand
Debounced Search Box Example🔍 appTimeline (300ms delay)'a'0msTimer 1Cancelled'p'100msTimer 2Cancelled'p'180msTimer 3API Call!480ms
Understanding the Visualization
1
User Types 'a'
Timer starts: API call scheduled for 300ms later
2
User Types 'ap'
Previous timer cancelled, new timer starts for 'ap'
3
User Types 'app'
Previous timer cancelled, new timer starts for 'app'
4
User Pauses
Timer completes - API call made for 'app'
Key Takeaway
🎯 Key Insight: Debouncing transforms rapid, chaotic events into single, meaningful actions by intelligently canceling and rescheduling execution.

Time & Space Complexity

Time Complexity
⏱️
O(1)

Each function call just sets/clears a timeout, which are constant time operations

n
2n
Linear Growth
Space Complexity
O(1)

Only stores a single timeout ID regardless of number of calls

n
2n
Linear Space

Constraints

  • 0 ≤ t ≤ 1000 (delay in milliseconds)
  • Function can be called with any number of arguments
  • Must preserve 'this' context of original function calls
  • Should handle rapid successive calls efficiently
Asked in
Google 85 Meta 72 Amazon 68 Microsoft 55 Netflix 43 Uber 38
89.2K Views
Very High Frequency
~15 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen