Skip to the page

Machine coding · Build it

Autocomplete / typeahead component

The interviewer asks

Create an autocomplete component that queries an API as the user types, with keyboard navigation and highlighting of the matched text.

Then

Follow-ups the interviewer asks next

  1. Debounce, cancellation and out-of-order responses.
  2. Cache previous queries (LRU), with a minimum query length.
  3. ARIA combobox semantics.
  4. Make it reusable: controlled/uncontrolled, render-prop for items, async vs static source.

The answer

What a strong answer covers

  • State: query, results, status, activeIndex, open. A debounced fetch with AbortController or a sequence guard.
  • Keyboard: ↑/↓/Enter/Escape, aria-activedescendant. Close on blur and outside click.
  • Highlight matches safely by splitting strings, not via innerHTML.
  • Empty, loading and error states. Cache keyed by normalised query.

The red pen

Common traps

Avoid:firing a fetch per keystroke; using innerHTML for highlighting; mouse-only UX.

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview