← WeRide Interview Insights

WeRide·Frontend Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Weride frontend interview, one coding round focused entirely on utility function implementation. The debounce question sounds routine until they add the options object and make you walk through both behaviors out loud.

Questions Asked (1)

Q1

Implement a debounce(fn, wait, options) utility in JavaScript, where options supports leading and trailing boolean flags to control when the function gets invoked. Walk through two test cases showing the difference in behavior between leading-only and trailing-only modes.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I started with the basic trailing version which went fine, but the options part tripped me up more than I expected.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the debounce behavior with leading and trailing options, then implement a closure that tracks the timer and whether the function has been called during the current wait period. Walk through two concrete test cases—one with leading=true, trailing=false and another with leading=false, trailing=true—to illustrate the invocation timing.

Pro tip: Mention that leading and trailing can both be true, causing the function to be invoked at the start and end of the wait period if called multiple times; this shows you understand edge cases and real-world usage.

1. Clarify requirements and edge cases

Confirm that options.leading and options.trailing are booleans, and discuss what happens if both are false (no invocation) or both are true (invoke at start and end).

2. Design the debounce function

Use a closure to maintain a timer ID and a flag to track if the function was called during the wait period. Return a new function that manages the timer and invokes fn based on the options.

3. Implement leading and trailing logic

On each call, clear the existing timer. If leading and it's the first call in the wait period, invoke fn immediately. Set a new timer that, after wait ms, invokes fn if trailing is true and there were calls during the wait.

4. Walk through test cases

For leading-only: call debounced function multiple times rapidly; show that fn is invoked only on the first call. For trailing-only: show that fn is invoked only after the wait period following the last call.

5. Discuss trade-offs and use cases

Explain when to use leading (e.g., immediate UI feedback) vs trailing (e.g., search suggestions after typing stops), and mention that both can be combined.

Key Points to Mention

  • Debounce delays function execution until after wait milliseconds have elapsed since the last call.
  • Leading option invokes the function on the leading edge of the wait period.
  • Trailing option invokes the function on the trailing edge of the wait period.
  • Both leading and trailing can be true, causing invocation at both edges if multiple calls occur.
  • Use clearTimeout and setTimeout to manage the timer.
  • Return a debounced function that preserves the original this context and arguments.

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