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 Leading Edge

50 views

Problem Statement

The default throttle behavior executes on the leading edge—immediately when called. However, explicitly understanding this behavior is crucial for building more configurable solutions. Implement a leading-edge throttle with clear semantics.

Requirements

Implement a throttleLeading(func, wait) function that:

  • Executes immediately on the first call (leading edge)
  • Ignores all calls within the wait period
  • Does NOT execute on the trailing edge (last queued call is dropped)
  • Resets the timer only when a call actually executes

Example Usage

const logClick = (buttonId) => {
  console.log(`Button ${buttonId} clicked at ${Date.now()}`);
};

const throttledClick = throttleLeading(logClick, 1000);

// Rapid clicks
throttledClick("submit");  // t=0ms    → Logs immediately
throttledClick("submit");  // t=100ms  → Ignored
throttledClick("submit");  // t=200ms  → Ignored
throttledClick("submit");  // t=900ms  → Ignored
throttledClick("submit");  // t=1100ms → Logs immediately (new window)
throttledClick("submit");  // t=1200ms → Ignored

Visual Timeline

Calls:    ▼  ▼  ▼     ▼        ▼  ▼  ▼
          0  100 200  500      1100 1200 1300  (ms)
          |__________|         |_________|
          Window 1             Window 2
          
Executes: ★                    ★
          ↑                    ↑
          Leading              Leading

Use Cases

  • Button click protection (instant feedback, prevent rapid clicks)
  • Keyboard shortcuts (respond immediately, ignore repeats)
  • Touch event handling on mobile

Follow-up Questions

  1. What are the downsides of dropping the trailing call?
  2. When might you prefer leading-only vs leading+trailing?

Sample Test Cases

Case 1
Input
{"delay": 1000, "calls": [0, 100, 200, 1500]}
Expected Output
[0,1500]
Case 2
Input
{"delay": 500, "calls": [0, 100, 600]}
Expected Output
[0,600]
Case 3
Input
["func", 100, [0, 50, 99, 100, 101, 199, 200, 201]]
Expected Output
[{"time":0,"args":[]},{"time":200,"args":[]}]
Case 4
Input
["func", 50, [0, 10, 20, 30, 40, 50, 51, 60, 70, 80, 90, 100]]
Expected Output
[{"time":0,"args":[]},{"time":50,"args":[]},{"time":100,"args":[]}]
Case 5
Input
["func", 200, [0, 201, 402]]
Expected Output
[{"time":0,"args":[]},{"time":201,"args":[]},{"time":402,"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
50
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering