Question presented to candidate: "Implement a throttle function from scratch. Walk me through why it caps the call rate, then show me it actually limiting a burst of rapid calls."
What a strong answer should cover:
- A closure holding a boolean cooldown flag (or, in an alternative implementation, a "last run timestamp").
- On each call: if currently in cooldown, ignore the call entirely; otherwise run the wrapped function immediately and enter cooldown for the configured interval.
- This means the FIRST call in a burst fires immediately (the "leading edge") by default, unlike debounce which always waits.
- The cooldown-flag version and the last-run-timestamp version are both correct, common implementations with a real tradeoff between them, not just stylistic variants.
- Real verification: proving with actual timestamps that a rapid burst produces a genuinely capped number of effect calls, spread at roughly the configured interval.
Clarifying questions expected:
- "Should this fire on the leading edge, the trailing edge, or both?" (a strong candidate flags that the simplest version only does leading, and that configurable edges are a natural, common follow-up)
Code / implementation expected: Yes — a full working throttle implementation, executed with a rapid-fire burst and real observed timestamps proving the capped call rate.