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
MediumJavaScript

Implement Debounce with Configurable Edges

41 views

Problem Statement

Different use cases require different debounce behaviors. Implement a flexible debounce function that supports configuration for both leading and trailing edge execution.

Requirements

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

  • leading (boolean, default: false) - invoke on the leading edge
  • trailing (boolean, default: true) - invoke on the trailing edge

Example Usage

const log = (msg) => console.log(msg);

// Trailing only (default)
const trailingDebounce = debounce(log, 100, { leading: false, trailing: true });
trailingDebounce("a");
trailingDebounce("b");
// After 100ms: logs "b"

// Leading only
const leadingDebounce = debounce(log, 100, { leading: true, trailing: false });
leadingDebounce("a"); // Logs "a" immediately
leadingDebounce("b"); // Ignored
// Nothing after 100ms

// Both edges
const bothDebounce = debounce(log, 100, { leading: true, trailing: true });
bothDebounce("a"); // Logs "a" immediately
bothDebounce("b");
bothDebounce("c");
// After 100ms: logs "c"

Edge Cases to Consider

  • What happens when both leading and trailing are false?
  • What if the function is called exactly once?
  • How do you handle rapid calls that span multiple wait periods?

Follow-up Questions

  1. How would Lodash's debounce handle this scenario?
  2. What are the tradeoffs of this API design?

Sample Test Cases

Case 1
Input
{"delay": 1000, "leading": true, "trailing": true, "calls": [0, 100, 200]}
Expected Output
[0,1200]
Case 2
Input
{"delay": 500, "leading": true, "trailing": false, "calls": [0, 100, 700]}
Expected Output
[0,700]
Case 3
Input
["log", 100, {"leading": false, "trailing": true}, [{"time": 0, "args": ["a"]}, {"time": 50, "args": ["b"]}, {"time": 101, "args": ["c"]}]]
Expected Output
{"calls": [{"time": 150, "args": ["b"]}, {"time": 201, "args": ["c"]}]}
Case 4
Input
["log", 100, {"leading": true, "trailing": false}, [{"time": 0, "args": ["a"]}, {"time": 50, "args": ["b"]}, {"time": 101, "args": ["c"]}]]
Expected Output
{"calls": [{"time": 0, "args": ["a"]}, {"time": 101, "args": ["c"]}]}
Case 5
Input
["log", 100, {"leading": true, "trailing": true}, [{"time": 0, "args": ["a"]}, {"time": 50, "args": ["b"]}, {"time": 101, "args": ["c"]}, {"time": 150, "args": ["d"]}]]
Expected Output
{"calls": [{"time": 0, "args": ["a"]}, {"time": 201, "args": ["c"]}, {"time": 250, "args": ["d"]}]}

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

Category

Frontend Engineering