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
EasyJavaScript

Implement a Basic Debounce Function

144 views

Problem Statement

As a Staff Engineer, I often see performance issues in production caused by excessive function calls during user interactions like typing, scrolling, or resizing. Your task is to implement a debounce utility function that limits how often a function can be executed.

Requirements

Implement a debounce(func, wait) function that:

  • Returns a new function that delays invoking func until after wait milliseconds have elapsed since the last time the debounced function was invoked
  • The debounced function should receive all arguments passed to it
  • The this context should be preserved

Example Usage

const searchAPI = (query) => {
  console.log(`Searching for: ${query}`);
};

const debouncedSearch = debounce(searchAPI, 300);

// User types quickly
debouncedSearch("h");
debouncedSearch("he");
debouncedSearch("hel");
debouncedSearch("hell");
debouncedSearch("hello");

// Only "Searching for: hello" is logged after 300ms

Real-world Context

At scale, this pattern is critical for:

  • Search-as-you-type features (reducing API calls)
  • Window resize handlers (preventing layout thrashing)
  • Auto-save functionality (batching saves)

Follow-up Questions

  1. What is the time and space complexity of your solution?
  2. How does debounce differ from throttle?
  3. When would you choose debounce over throttle?

Sample Test Cases

Case 1
Input
{"delay": 1000, "calls": [0, 100, 200], "waitUntil": 1500}
Expected Output
[1200]
Case 2
Input
{"delay": 500, "calls": [0, 600, 700], "waitUntil": 1500}
Expected Output
[500,1200]
Case 3
Input
{"func": "(a, b) => a + b", "wait": 100, "calls": [{"args": [1, 2], "delay": 0}, {"args": [3, 4], "delay": 50}, {"args": [5, 6], "delay": 120}], "expected_call_args": [[5, 6]]}
Expected Output
{"callCount": 1, "args": [[5, 6]]}
Case 4
Input
{"func": "(x) => x * 2", "wait": 50, "calls": [{"args": [10], "delay": 0}, {"args": [20], "delay": 60}, {"args": [30], "delay": 120}], "expected_call_args": [[30]]}
Expected Output
{"callCount": 1, "args": [[30]]}
Case 5
Input
{"func": "() => 'hello'", "wait": 200, "calls": [{"args": [], "delay": 0}, {"args": [], "delay": 100}, {"args": [], "delay": 300}], "expected_call_args": [[]]}
Expected Output
{"callCount": 1, "args": [[]]}

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
144
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering