gitGood.dev
← Back to problems

Frontend: Debounced Search Input (Cancellation)

MediumTypeScript

Description

> This problem is paper-only - see explanation for the canonical React solution. A search input fires the API once per keystroke is wasteful and creates race conditions (older slower responses overwriting newer faster ones). The fix: debounce the input AND cancel/ignore stale responses. **For the auto-grader:** implement `shouldKeepResponse(requestId, latestRequestId)`. Return `true` only if the response is for the latest request. (Race-condition guard.)
Example 1
Input: requestId = 5, latestRequestId = 5
Output: true
Example 2
Input: requestId = 4, latestRequestId = 5
Output: false
Explanation: Stale response - newer query in flight.

Constraints

- requestIds are positive integers, monotonically increasing.
Loading editor...