Skip to solution
hardFrontend

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

332 views
01

Understand 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.

utilitytrickyreal-world
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript / frontend-implementation interview questions. Difficulty: Hard

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result below was actually run on Node.js v24.19.0 — the printed

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

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

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 153 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track