Skip to the solution
MediumMachine Coding9 min read

Build Autocomplete / Typeahead with Debounce & Highlight

49 views

The problem

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 mark elements, 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 AbortController plus 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 (r slow, re slower than rea).
  • 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] or dangerouslySetInnerHTML.
  • Why lowercasing and slicing by indexOf can 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.

  • React
  • Autocomplete
  • Typeahead
  • Debounce
  • Accessibility

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

9 min read Β· 9 sections

React solution

Every file of the working solution. Open it in the playground to run and change it.

ReactComplete solution
import Typeahead from "./src/components/Typeahead";

export default function App() {
  return (
    <main className="page">
      <h1>Package search</h1>
      <Typeahead />
    </main>
  );
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 77 of 107 in Machine coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

Build an E-commerce Product Filter grid

Continue