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

61 views

Problem Statement

Some scenarios require guaranteed execution of the most recent call, even if it occurs during a throttle window. Implement a trailing-edge throttle that captures and executes the last call after the wait period.

Requirements

Implement a throttleTrailing(func, wait) function that:

  • Does NOT execute immediately on the first call
  • Queues the most recent call during the wait period
  • Executes the queued call at the end of each wait period
  • If no calls occur during a wait period, nothing executes

Example Usage

const updatePosition = (x, y) => {
  console.log(`Position: (${x}, ${y})`);
};

const throttledUpdate = throttleTrailing(updatePosition, 100);

// Mouse moves rapidly
throttledUpdate(10, 20);   // t=0ms   → Queued
throttledUpdate(15, 25);   // t=20ms  → Replaces queue
throttledUpdate(20, 30);   // t=50ms  → Replaces queue
// t=100ms → Logs: "Position: (20, 30)"

throttledUpdate(25, 35);   // t=120ms → Queued
throttledUpdate(30, 40);   // t=150ms → Replaces queue
// t=200ms → Logs: "Position: (30, 40)"

Visual Timeline

Calls:    ▼  ▼  ▼              ▼  ▼
          0  20 50             120 150         (ms)
          |________|           |_____|
          Window 1             Window 2
          
Executes:          ★                  ★
                   ↑                  ↑
                   Trailing           Trailing
                   (last value)       (last value)

Use Cases

  • Syncing final position after drag operations
  • Ensuring the last form state is saved
  • Analytics that need the final interaction state

Follow-up Questions

  1. How would you handle arguments for the trailing call?
  2. What happens if the user stops interacting mid-window?

Sample Test Cases

Case 1
Input
{"delay": 1000, "calls": [0, 100, 200]}
Expected Output
[1000]
Case 2
Input
[
  (x, y) => `Position: (${x}, ${y})`,
  100,
  [
    { time: 0, args: [10, 20] },
    { time: 20, args: [15, 25] },
    { time: 50, args: [20, 30] },
    { time: 120, args: [25, 35] },
    { time: 150, args: [30, 40] }
  ]
]
Expected Output
[
  { time: 100, args: [20, 30] },
  { time: 200, args: [30, 40] }
]
Case 3
Input
{"delay": 500, "calls": [0, 100, 600, 700]}
Expected Output
[500,1100]
Case 4
Input
[
  (val) => `Value: ${val}`,
  50,
  [
    { time: 0, args: [1] },
    { time: 10, args: [2] },
    { time: 60, args: [3] },
    { time: 70, args: [4] }
  ]
]
Expected Output
[
  { time: 50, args: [2] },
  { time: 110, args: [4] }
]
Case 5
Input
[
  () => 'No args',
  200,
  [
    { time: 0, args: [] },
    { time: 50, args: [] },
    { time: 250, args: [] }
  ]
]
Expected Output
[
  { time: 200, args: [] },
  { time: 450, 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
61
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering