Autocomplete data fetching with debounce, retry and caching
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Databricks's algorithms (two rounds) round, last asked Apr 2026. It tests frontend, async and debounce.
The UI of an autocomplete input is already built. Write only the data-fetching logic behind it: given the current query, fetch suggestions with debouncing, so that the latest query always wins, transient failures are retried, and recent results are cached. Treat every timer, request and retry as something that can be cancelled, and never update state after the component unmounts.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
useSuggestions(query: string, fetchSuggestions: (q: string, signal: AbortSignal) => Promise<string[]>, options?)returns{ suggestions: string[], loading: boolean, error: Error | null }. -
options:debounceMs(default 300),maxRetries(default 2),cacheTtlMs(default 60,000),cacheSize(default 100).
Part 1: debounce
Send a request only after the query has been unchanged for debounceMs. An empty or whitespace-only query clears the suggestions and sends nothing.
t=0 "r"
t=100 "re"
t=200 "rea"
t=500 fetch("rea") # the only request: 300 ms after the last keystroke
Part 2: latest result wins
Responses can arrive out of order. A response for an older query must never replace the suggestions for a newer one. Abort the old request where possible, and check the query identity when a response arrives.
# debounce left out to keep the timeline short
t=0 fetch("re") starts
t=50 fetch("rea") starts # "re" is aborted or ignored
t=120 "rea" answers -> shown
t=200 "re" answers -> ignored
Part 3: retry transient failures
Retry only transient errors (network errors, 5xx, 429), at most maxRetries times, with exponential backoff and jitter. A new query cancels the whole retry sequence of the old one, including a pending backoff sleep.
Part 4: cache
Reuse recent successful results by normalized query (trimmed, lowercased). Bound the cache by size (least recently used) and age (TTL). Decide whether a stale entry is shown while a fresh request runs (stale-while-revalidate), and never hand out a shared mutable array.
Constraints
- Use fake timers or an injected clock so every part can be tested.
- No polling.
Clarifying questions
- Should the debounce delay apply to the first keystroke, or should the first request go out at once?
- Which errors count as transient? Does the server send
Retry-After? - Should cached results show instantly, even if stale?
- Is loading shown during the debounce wait or only while fetching?
What a strong answer covers
- Debouncing the request, not the rendering, with the timer cleared on every query change and on unmount.
- A request id or
AbortControllerper query, so stale responses and errors are dropped and loading and error states change only for the current query. - Error classification, bounded backoff with jitter, and cancellation during both the sleep and the fetch.
- A normalized cache key, TTL and LRU eviction, copies of results, and cache policy kept apart from UI code.
- Tests with fake timers for each part: typing fast, out-of-order answers, a failure then success, a cache hit, unmount mid-request.
Follow-ups
- Keep screen-reader announcements correct while results update.
- Deduplicate identical in-flight queries across several components.
- Add client-side ranking without blocking typing.
Where it's asked
- Databricks · Algorithms (two rounds) · Last asked Apr 202660 min · Live coding in CoderPad or similar
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
