I started with the basic trailing version which went fine, but the options part tripped me up more than I expected.
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.
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).
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.
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.
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.
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.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.