DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.

← Back to Questions
HardJavaScript

Implement Debounce with Maximum Wait Time

372 views

Problem Statement

Standard debounce can potentially never execute if calls keep coming. In critical applications, we need a guarantee that the function will execute within a maximum time window. Implement a debounce with maxWait.

Requirements

Implement a debounce(func, wait, options) function where:

  • maxWait (number) - maximum time the function can be delayed
  • If maxWait is reached, function executes regardless of ongoing calls
  • After maxWait execution, the cycle resets
  • Standard debounce behavior applies within maxWait window

Example Usage

const sendAnalytics = (events) => {
  console.log(`Sending ${events.length} events at ${Date.now()}`);
};

// Debounce 100ms, but guarantee execution every 1 second
const debouncedAnalytics = debounce(sendAnalytics, 100, { maxWait: 1000 });

// Continuous stream of events (every 50ms for 3 seconds)
let events = [];
const interval = setInterval(() => {
  events.push({ type: "click", time: Date.now() });
  debouncedAnalytics(events);
}, 50);

// Without maxWait: nothing would execute during the 3 seconds
// With maxWait: executes at ~1000ms, ~2000ms, and ~3100ms (after interval stops)

setTimeout(() => clearInterval(interval), 3000);

Real-world Context

  • Analytics event batching with SLA guarantees
  • Real-time collaboration (periodic sync even during active editing)
  • Progress indicators that must update within reasonable time

Follow-up Questions

  1. How does this interact with leading/trailing options?
  2. What's the relationship between wait and maxWait values?
  3. How would you test the maxWait behavior?

Sample Test Cases

Case 1
Input
{"delay": 1000, "maxWait": 2000, "calls": [0, 500, 1000, 1500, 2500]}
Expected Output
[2000,3500]
Case 2
Input
{"delay": 500, "maxWait": 1000, "calls": [0, 300, 600, 900]}
Expected Output
[1000]
Case 3
Input
[
  (args) => {
    if (!this.calls) this.calls = [];
    this.calls.push({ args, time: Date.now() });
  },
  100,
  { maxWait: 300 },
  [
    { action: 'call', delay: 0, args: [1] },
    { action: 'call', delay: 50, args: [2] },
    { action: 'call', delay: 100, args: [3] },
    { action: 'advanceTimersByTime', delay: 100 },
    { action: 'call', delay: 0, args: [4] },
    { action: 'advanceTimersByTime', delay: 100 },
    { action: 'call', delay: 0, args: [5] },
    { action: 'advanceTimersByTime', delay: 100 },
    { action: 'call', delay: 0, args: [6] },
    { action: 'advanceTimersByTime', delay: 500 }
  ]
]
Expected Output
[
  {"args":[[3]],"time":300},
  {"args":[[5]],"time":600},
  {"args":[[6]],"time":1100}
]
Case 4
Input
[
  (args) => {
    if (!this.calls) this.calls = [];
    this.calls.push({ args, time: Date.now() });
  },
  200,
  { maxWait: 500 },
  [
    { action: 'call', delay: 0, args: ['a'] },
    { action: 'advanceTimersByTime', delay: 100 },
    { action: 'call', delay: 0, args: ['b'] },
    { action: 'advanceTimersByTime', delay: 100 },
    { action: 'call', delay: 0, args: ['c'] },
    { action: 'advanceTimersByTime', delay: 100 },
    { action: 'call', delay: 0, args: ['d'] },
    { action: 'advanceTimersByTime', delay: 100 },
    { action: 'call', delay: 0, args: ['e'] },
    { action: 'advanceTimersByTime', delay: 1000 }
  ]
]
Expected Output
[
  {"args":[["c"]],"time":400},
  {"args":[["e"]],"time":600},
  {"args":[["e"]],"time":1600}
]
Case 5
Input
[
  (args) => {
    if (!this.calls) this.calls = [];
    this.calls.push({ args, time: Date.now() });
  },
  50,
  { maxWait: 150 },
  [
    { action: 'call', delay: 0, args: [1] },
    { action: 'advanceTimersByTime', delay: 10 },
    { action: 'call', delay: 0, args: [2] },
    { action: 'advanceTimersByTime', delay: 10 },
    { action: 'call', delay: 0, args: [3] },
    { action: 'advanceTimersByTime', delay: 10 },
    { action: 'call', delay: 0, args: [4] },
    { action: 'advanceTimersByTime', delay: 10 },
    { action: 'call', delay: 0, args: [5] },
    { action: 'advanceTimersByTime', delay: 10 },
    { action: 'call', delay: 0, args: [6] },
    { action: 'advanceTimersByTime', delay: 10 },
    { action: 'call', delay: 0, args: [7] },
    { action: 'advanceTimersByTime', delay: 10 },
    { action: 'call', delay: 0, args: [8] },
    { action: 'advanceTimersByTime', delay: 10 },
    { action: 'call', delay: 0, args: [9] },
    { action: 'advanceTimersByTime', delay: 100 }
  ]
]
Expected Output
[
  {"args":[[3]],"time":60},
  {"args":[[5]],"time":110},
  {"args":[[9]],"time":200},
  {"args":[[9]],"time":300}
]

No solutions yet

Be the first to share a solution for this question.

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Stats

Views
372
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering