← ByteDance Interview Insights

ByteDance·Frontend Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Mar 2026

Summary

Bytedance frontend round, got a throttle implementation question with a twist I didn't fully anticipate going in. The trailing-call behavior is what separates this from a basic throttle and that's clearly the point.

Questions Asked (1)

Q1

Implement a throttle function where the callback fires at most once per time window, but any call made during an active window must be remembered so its arguments are used to fire the callback at the end of that window.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I got the basic throttle part fine, set a flag, run the callback, block subsequent calls.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements: throttle with trailing edge invocation, where calls during the cooldown are stored and the last one fires at the end. Then implement using a timer and a stored arguments variable, ensuring the timer resets appropriately. Finally, discuss edge cases like multiple calls and cancellation.

Pro tip: Mention that you'd use a timestamp-based approach for the leading edge and a timer for the trailing edge, and that you'd clear the timer if a new call comes in after the window ends to avoid stale invocations.

1. Clarify requirements and edge cases

Confirm that the throttle should fire immediately on the first call (leading edge) and also fire at the end of the window if there were calls during it (trailing edge). Ask about behavior when multiple calls occur during the window—only the last arguments should be used.

2. Design the state variables

Use a boolean flag (e.g., waiting) to track if we're in a cooldown period, and a variable (e.g., trailingArgs) to store the arguments of the most recent call during the window. Optionally, store the timer ID for cleanup.

3. Implement the throttle logic

On each call, if not waiting, invoke the callback immediately with the arguments, set waiting to true, and start a timer for the delay. If waiting, update trailingArgs with the current arguments. When the timer fires, if trailingArgs exists, invoke the callback with those arguments, reset trailingArgs, and restart the timer if needed; otherwise, set waiting to false.

4. Handle cancellation and cleanup

Provide a cancel method that clears the timer, resets waiting to false, and clears trailingArgs. This prevents memory leaks and allows the throttle to be stopped.

5. Test with scenarios

Walk through examples: rapid calls within the window, calls spaced exactly at the boundary, and calls after a long pause. Verify that the callback fires at most once per window and that the last arguments are used for the trailing call.

Key Points to Mention

  • Difference between throttle and debounce, and why throttle is needed here.
  • Leading and trailing edge invocation, and how to support both.
  • Using a timer (setTimeout) to schedule the trailing call.
  • Storing the latest arguments to use in the trailing call.
  • Handling the case where a call occurs exactly when the timer fires (race condition).
  • Providing a cancel method to clean up timers and state.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.