Skip to the solution
HardFrontend7 min read

How would you implement a throttle function with configurable leading- and trailing-edge execution?

332 views

The problem

Question presented to candidate: "Extend a basic throttle so the caller can independently configure whether it fires on the leading edge, the trailing edge, or both. Show me all four combinations actually behaving differently."

What a strong answer should cover:

  • The base throttle mechanism (a cooldown window) stays the same; leading/trailing only control WHICH edges of that window actually invoke the function.
  • leading: true (the default in most implementations): the function runs immediately on the first call of a burst.
  • trailing: true (also usually the default): if calls arrive during the cooldown, one final call runs once the cooldown expires, using the most recent arguments seen.
  • {leading:false, trailing:false} is a real, documented edge case β€” a correctly-implemented throttle with BOTH disabled genuinely never invokes the function at all, which surprises people who expect it to still fire something.
  • Real verification: proving with real timing that all four combinations produce genuinely different call counts and timing, not just assuming the flags work from reading the option names.

Clarifying questions expected:

  • "What should the default be if leading/trailing are not specified?" (a strong candidate proposes both true by default, matching common libraries like lodash and underscore)
  • "For the trailing call, which call's arguments should it use β€” the first one dropped during cooldown, or the most recent one?" (the most recent is standard, but worth confirming)

Code / implementation expected: Yes β€” a full working configurable-throttle implementation, executed against all 4 leading/trailing combinations with real observed call counts and timestamps.

  • utility
  • tricky
  • real-world

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 7 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptConfigurable leading/trailing throttle: all 4 combinations (run directly)

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 153 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

How would you transfer a large ArrayBuffer between a worker and the main thread without copying it, using postMessage's transfer list?

Continue