Skip to the solution
MediumMachine Coding10 min read

Build an Autocomplete / Typeahead search

744 views

The problem

Question presented to candidate: "Build a country field that suggests matches as the user types. Suggestions come from an API. The user should be able to pick one with the arrow keys and Enter, or with the mouse. Do not call the API for a query we already asked about."

Requirements:

  • Fetch suggestions (at most 6, prefix matches first) about 250 ms after the user stops typing.
  • Show a dropdown with Loading / No matches / suggestion states.
  • Keyboard: Down / Up move the highlight and wrap around, Enter picks the highlighted suggestion, Escape closes.
  • Mouse: hovering highlights, clicking picks, and the input keeps focus.
  • Cache results per query so going back to an earlier query (for example with Backspace) does not hit the API again; ignore a response if a newer lookup has started.
  • Show the picked value and a small counter of requests sent and cache hits. The API is simulated.

What a strong answer should cover:

  • State model: input text, open flag, suggestions, active index, picked value; the active index reset whenever suggestions change.
  • Debounce reused from a small hook / composable, and a lookup that only runs when the dropdown is open and the debounced value has caught up.
  • A Map cache keyed by the normalised query, and a "latest request id" check so an older response cannot replace newer suggestions.
  • Keyboard handling on the input with wrap-around, mousedown preventDefault so clicking an option does not blur the input, and closing on blur.
  • Basic combobox ARIA: role="combobox", aria-expanded, aria-controls, aria-activedescendant, role="listbox" / role="option".

Clarifying questions expected:

  • "Is the list small enough to filter on the client, or must every query go to the server?"
  • "Should picking a suggestion only fill the input, or also navigate somewhere?"
  • "How long may cached results be reused?"

Code / implementation expected: Yes. A working autocomplete with debounced fetching, a cache and full keyboard and mouse selection.

  • React
  • Async
  • Debounce

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

10 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 Autocomplete from "./src/components/Autocomplete";

export default function App() {
  return (
    <main className="page">
      <h1>Country finder</h1>
      <Autocomplete />
    </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 44 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 nextMedium

Build a Flight Booker

Continue