Problem 6
Debounced Search with Stale Response Protection
MEDIUMBUILD
React+2
ReactTypeScriptAsync State
Implement a debounced search component in React + TypeScript. You are given search(query: string): Promise<Result[]> (where Result = { id: string; label: string }) and a debounceMs prop. Render data-testid="search-input" and data-testid="results" with each result as data-testid="result-item". Only call search after the user has stopped typing for debounceMs. If multiple requests are in flight, only the most recent response may be rendered — earlier responses must be discarded.
Requirements
- Keep
Appas the default exported React component withsearchanddebounceMsprops. - Render
search-input,results, and oneresult-itemper visible result. - Do not call
searchuntil the user has stopped typing fordebounceMs. - The initial results area must be empty.
- If responses resolve out of order, render only the latest query result.
- Earlier in-flight responses must not overwrite newer results.
Examples
Example 1
Input
debounceMs = 200
User types "c" then quickly "ca"
search("c") resolves after search("ca")Output
Rendered results reflect the "ca" response only
Constraints
- search may resolve in any order — stale results must never overwrite newer ones
- 0 <= debounceMs <= 2000
Follow-up
Can you avoid calling search when the trimmed query is empty, while keeping the same UI behavior?
Hints
Related Practice
Track
Backend Basics
Keep moving through related backend basics problems and build a stronger search-friendly practice loop around this topic.
View track →http://localhost:3000/preview
Console output will appear here...