Question presented to candidate: "You already have a basic typeahead. Now make it production-safe: highlight the part of each suggestion that matches what the user typed, without ever injecting HTML, and make sure a slow, older response can never replace the results for what is in the box right now. Show me the race happening and show me your fix."
Requirements:
- Debounce input by 300 ms and fetch suggestions from a simulated API whose latency depends on the query, so older requests can resolve last.
- Highlight every case-insensitive occurrence of the query inside each suggestion with
markelements, treating the query as literal text:c++,(and.must not break or change matching. - Render suggestions as text: an item containing markup such as
<img src=x onerror=...>must be displayed, never parsed. - Guard against out-of-order responses with
AbortControllerplus a request-id check; include a toggle that turns the guard off so the bug can be demonstrated. - Keyboard Up / Down / Enter / Escape and
aria-activedescendant, as in the basic version.
What a strong answer should cover:
- Why debounce alone does not order responses, with a concrete timeline (
rslow,reslower thanrea). - Two layers of protection: abort the previous request, and still ignore any response whose id is not the latest (for APIs that ignore abort).
- Highlighting by splitting on an escaped regex with one capture group, then rendering parts as text nodes; no
innerHTML,v-html,[innerHTML]ordangerouslySetInnerHTML. - Why lowercasing and slicing by
indexOfcan misalign (Δ°lowercases to two characters). - Showing which query the visible results belong to, and testing the race deterministically.
Clarifying questions expected:
- "Can the API be cancelled, or must I ignore stale responses client-side?"
- "Should highlighting match whole words, prefixes, or any substring? Should it ignore accents?"
- "Can suggestion text contain user-generated content?"
Code / implementation expected: Yes. A working typeahead with safe highlighting and a demonstrable race guard.