Home
← All questions

Typeahead / autocomplete component | Frontend Machine Coding

MediumPremiumMachine Coding

Frontend Machine Coding interview practice: Build a debounced autocomplete input with async suggestions and keyboard navigation.

Problem

Build a typeahead/autocomplete input that fetches suggestions as the user types.

Requirements

  • Debounce input before fetching; show a suggestions dropdown.
  • Handle loading and empty states; close on outside click / Escape.
  • Keyboard navigation: Up/Down to move, Enter to select.
  • (Bonus) Cache results per query, cancel stale in-flight requests, highlight the matched substring.

Hints / Approach

  • Debounce the query (e.g. 300ms) to limit requests.
  • Guard against race conditions: track the latest request (AbortController or a request id) so a slow earlier response doesn't overwrite a newer one.
  • Track highlightedIndex for keyboard nav; reuse an LRU cache (see "TypeAhead cache with LRU eviction").
Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium