Build a React Autocomplete Search Bar

intermediate
By FrontendAtlas Editorial · Updated Nov 22, 2026

Intermediate Frontend machine coding interview question. Recommended time: 35-45 minutes.

35-45 min intermediate level
React state/effectsDebounceAsync race handlingKeyboard navigationFocus managementAccessibilityTests
  • Debounced async search and stale-response protection
  • Keyboard navigation, focus management, and ARIA combobox contract
  • Loading, empty, no-results, error, pointer, and cleanup edge cases
Practice a React autocomplete interview question in a live editor. Build a controlled search input with debounce, stale-response protection, keyboard navigation, ARIA, loading, empty, no-results, error states, and runnable tests for a frontend coding interview.

What you'll practice:

  • Frontend machine coding interview question at intermediate level.
  • Recommended time: 35-45 minutes.
  • Build an autocomplete search bar in React, then run checks against visible behavior.

Requirements

  • Render a single App component as the main UI for this first version.
  • Use a controlled query input and keep the query as React state.
  • Debounce search work before filtering suggestions.
  • Simulate latency in searchCities: 900 ms for the query san and 50 ms for every other query.
  • Filter suggestions case-insensitively by substring match.
  • Show a loading state while the debounced async search is pending.
  • Close the popup and clear results for an empty or whitespace-only query.
  • Show a no-results state when a valid query returns no matches.
  • Show a deterministic error state for a known failing query such as error.
  • Use ArrowDown and ArrowUp with wrap-around navigation.
  • Use Enter to select the active option and Escape to close the popup without changing the query.
  • Close the dropdown on outside pointer interaction.
  • Handle option selection with pointer events so mouse, pen, and touch activation occurs before blur or outside-close timing can swallow it.
  • Clear old options immediately when the query changes, prevent selection while loading, and prevent stale slow responses from overwriting newer results.
  • Clean up timers and document listeners when effects rerun or the component unmounts.
  • Give the combobox an explicit accessible name with a visible label associated through for/id (and aria-labelledby when used).

What the checks look for

The checks find elements by .input, .option, .status, #autocomplete-option-seattle, .list, mark, #autocomplete-option-san-jose, body, label[for="autocomplete-query"], #autocomplete-listbox, #autocomplete-option-san-diego. Keep these in your markup.

Acceptance criteria

  • Keep the first implementation in one component; extract pieces only as a follow-up.
  • Match highlighting uses React text nodes and mark, not HTML string injection.
  • You can explain how clearing the debounce timer differs from cancelling a request that has already started.

Expected behavior

The autocomplete should behave like an editable combobox with list autocomplete.

  • Typing a non-empty query opens the popup, sets loading, then shows matching suggestions after the debounce and fake async search complete.
  • Whitespace-only input closes the popup, clears suggestions, and returns to idle.
  • A query with no matches shows No results rather than an error.
  • The error query shows an error message and does not also show No results.
  • ArrowDown from no active option selects the first option; ArrowUp selects the last option; both wrap around.
  • Enter selects the active option, writes it to the input, and closes the popup.
  • Escape closes the popup while preserving the current query.
  • Pointer selection works for mouse, pen, and touch even when the input would otherwise blur.
  • Typing san and then se makes the old 900 ms san response finish after the 50 ms se results; the se results stay visible.
  • When a new non-empty query enters loading, old options disappear immediately and Arrow/Enter cannot select them.

Implementation notes

  • Use query, results, status, isOpen, activeIndex, and a request sequence ref as the core state model.
  • Use setTimeout cleanup to cancel pending debounce work before it starts a request.
  • Use both request identity and AbortController: abort cancellable work, and still guard every async continuation against stale request identity.
  • Clear results and activeIndex in the input-change transition as well as the effect, so passive-effect timing cannot leave stale options interactive.
  • Associate a visible label with the input, then put role="combobox", aria-expanded, aria-autocomplete="list", aria-controls, and aria-activedescendant on it.
  • Put role="listbox" on the popup and role="option" with stable ids and aria-selected on every result.
  • Keep DOM focus on the input while keyboard navigation changes activeIndex.
  • Use pointer events for option activation as an alternative path; keyboard interaction must remain complete and focus stays on the input.

Common mistakes

  • Saying that clearing the debounce timer cancels an already-started request.
  • Using dangerouslySetInnerHTML or HTML strings for highlighting matched text.
  • Using mouse-only or click-only option selection instead of a pointer event that covers mouse, pen, and touch before blur/outside-close races.
  • Moving DOM focus into list options instead of managing aria-activedescendant on the input.
  • Letting an older slow response overwrite results for a newer query.
  • Leaving successful results mounted and selectable while a different query is loading.

Interview explanation

Interviewers evaluate visible correctness, async safety, keyboard and focus behavior, accessibility contracts, tests, and whether you can explain the trade-offs without over-engineering the first version.

Testing checklist

  • Empty or whitespace query clears results and closes the popup.
  • Rapid typing only starts the expected final debounced search.
  • A stale slow response cannot overwrite newer results.
  • Loading opens and closes correctly.
  • Error and no-results states are distinct.
  • ArrowDown and ArrowUp wrap around.
  • Enter selects the active option.
  • Escape closes without changing the query.
  • Outside interaction closes the popup.
  • Pointer option selection covers mouse, pen, and touch semantics and survives blur/outside-close races.
  • aria-controls, aria-activedescendant, and option ids are connected.
  • Unmount leaves no pending debounce timer or document listener.
  • Changing the query clears old options synchronously; ArrowDown and Enter cannot select while loading.
  • The visible label provides the combobox accessible name.

FAQ

Is this the same intent as debounced search?

No. Debounced search practices async search state in general; this challenge owns the React autocomplete implementation with combobox behavior, keyboard navigation, ARIA, and tests.

Should I extract hooks first?

No for the first interview pass. Keep one component until behavior is correct, then discuss hook or component extraction as a senior follow-up.

Solution idea

State model

  • Source state: query, results, status, isOpen, activeIndex, and requestSeqRef.
  • Derived state: trimmedQuery, hasResults, active option id, and the visible status text.
  • status should be one of idle, loading, success, empty, or error so loading, no-results, and error cannot collapse into the same branch.

Public interview reports

These public, self-reported interview accounts informed this practice version. The exercise is modelled on reported rounds, not copied from them, and the reports are not verified official questions or endorsements.

Reported in a public Waymo interview account. Waymo frontend interview questions

Reported in a public ElevenLabs interview account. ElevenLabs frontend interview questions

Reported in a public Intuit interview account. Intuit frontend interview questions

Reported in a public Google interview account. Google frontend interview questions

Reported in a public Zalando interview account. Zalando frontend interview questions

Reported in a public Booking.com interview account. Booking.com frontend interview questions