← Weride Interview Insights

Weride·Frontend Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Weride frontend interview had me implementing a debounce utility from scratch, which sounds straightforward until they ask you to layer in leading/trailing edge options and walk through test cases live. Pretty focused session, no behavioral fluff.

Questions Asked (1)

Q1

Implement a debounce(fn, wait, options) function in JavaScript, where options controls whether fn fires on the leading edge, trailing edge, or both.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Started with the basic version fine, delay the call, cancel if another comes in before wait ms.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the expected behavior for leading/trailing options and edge cases like immediate invocation and cancellation. Then outline a closure-based implementation using a timer variable and a flag to track the last call time, and finally walk through the code logic for each option combination.

Pro tip: Mention that debounce should return a function with a cancel method to clear pending invocations, and that you'd use Date.now() or performance.now() for precise timing—this shows production awareness beyond the basic implementation.

1. Clarify requirements and edge cases

Ask about the exact semantics of leading/trailing options, whether the function should be called with the latest arguments, and if cancellation or flushing is needed. Confirm that wait is in milliseconds and that the returned function should preserve this context.

2. Design the closure structure

Plan to use a closure that maintains a timerId, lastCallTime, and lastArgs. Explain how you'll track whether the debounced function is currently in a 'waiting' state to decide leading/trailing invocations.

3. Implement the core logic

Write the debounced function that clears any existing timer, checks if it's the leading edge (first call after wait), and schedules the trailing call. Use setTimeout and clearTimeout to manage the timer.

4. Handle options and cancellation

Add branching for leading, trailing, or both. Implement a cancel method that clears the timer and resets state. Optionally add a flush method to invoke immediately.

5. Test and discuss trade-offs

Walk through test cases: rapid calls, calls spaced beyond wait, leading-only, trailing-only, and both. Discuss trade-offs like memory usage, timer precision, and whether to use requestAnimationFrame for UI events.

Key Points to Mention

  • Closure to encapsulate timer and state variables
  • Leading edge invocation on the first call, trailing on the last call after wait
  • Using clearTimeout and setTimeout to reset the timer
  • Preserving the original function's this context and arguments
  • Providing a cancel method to clear pending invocations
  • Edge cases: immediate subsequent calls, wait=0, and options combinations

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