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 Throttle with Configurable Leading and Trailing

34 views

Problem Statement

A production-grade throttle should support both leading and trailing edge execution. This provides immediate response AND ensures the final state is captured. Implement a fully configurable throttle.

Requirements

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

  • leading (boolean, default: true) - execute on leading edge
  • trailing (boolean, default: true) - execute on trailing edge
  • Both can be true simultaneously (default Lodash behavior)
  • At least one must be true (validate this)

Example Usage

const track = (value) => console.log(`Value: ${value}`);

// Both edges (default) - most common in production
const bothThrottle = throttle(track, 100, { leading: true, trailing: true });
bothThrottle("a"); // t=0   → Logs "a" immediately
bothThrottle("b"); // t=30  → Queued
bothThrottle("c"); // t=60  → Replaces queue
// t=100 → Logs "c" (trailing)

// Leading only
const leadingThrottle = throttle(track, 100, { leading: true, trailing: false });
leadingThrottle("a"); // Logs immediately
leadingThrottle("b"); // Ignored
leadingThrottle("c"); // Ignored
// Nothing at t=100

// Trailing only
const trailingThrottle = throttle(track, 100, { leading: false, trailing: true });
trailingThrottle("a"); // Queued
trailingThrottle("b"); // Replaces queue
// t=100 → Logs "b"

Behavior Matrix

LeadingTrailingFirst CallEnd of Window
truetrueExecutesExecutes (if queued)
truefalseExecutesNothing
falsetrueQueuedExecutes
falsefalseInvalid - throw error

Follow-up Questions

  1. How does this compare to Lodash's throttle implementation?
  2. What edge cases exist when both options are true?

Sample Test Cases

Case 1
Input
{"delay": 1000, "leading": true, "trailing": true, "calls": [0, 100, 200]}
Expected Output
[0,1000]
Case 2
Input
["func", 100, {"leading": true, "trailing": true}, [{"args": ["a"], "time": 0}, {"args": ["b"], "time": 30}, {"args": ["c"], "time": 60}, {"args": ["d"], "time": 110}, {"args": ["e"], "time": 150}]]
Expected Output
["a", "c", "d", "e"]
Case 3
Input
{"delay": 500, "leading": true, "trailing": false, "calls": [0, 100, 600]}
Expected Output
[0,600]
Case 4
Input
["func", 100, {"leading": true, "trailing": false}, [{"args": ["a"], "time": 0}, {"args": ["b"], "time": 30}, {"args": ["c"], "time": 60}, {"args": ["d"], "time": 110}]]
Expected Output
["a", "d"]
Case 5
Input
["func", 100, {"leading": false, "trailing": true}, [{"args": ["a"], "time": 0}, {"args": ["b"], "time": 30}, {"args": ["c"], "time": 60}, {"args": ["d"], "time": 110}]]
Expected Output
["c", "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
34
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering