Skip to content

How to design an autocomplete search box (frontend)

The first sections are free. The deep dives, level expectations and quiz are part of Pro.

On this page
Practice this question

The problem

A search box that suggests results as someone types, built once and dropped into many teams’ pages. The answer turns on one fact: people type faster than the server replies, so the box must decide which answer is current while the input itself never waits.

Clarifying questions

AskWhy it mattersA typical answer

Where do the suggestions come from: a list already in the page, or a call to a server?

A list in memory can be filtered on every key with no network at all. A server call brings waiting, failures and answers in the wrong order, which is most of this question.

A server call. Each team passes a function that fetches suggestions for a query, and the default calls GET /search?q=…. How the server finds and ranks matches is not your problem today.

How fast does the server answer?

The wait before asking, and whether a loading state is ever seen, both depend on it.

Say under 100 milliseconds when it is fast and 300 milliseconds or more when it is slow. Each keystroke itself must show within 200 milliseconds.

Does Enter search for the typed text, or only pick a highlighted suggestion?

It decides whether the box keeps the typed text as the value or takes the first suggestion for you, and what Enter does with nothing highlighted.

Enter picks the highlighted suggestion and calls the team’s handler. With nothing highlighted, the typed text stays as typed. The search results page after a pick belongs to another team.

Who uses it, and on which devices?

Keyboard and screen reader support change the markup from the start, and phones have smaller screens and slower processors.

Phones and desktop browsers. It must work with only a keyboard and with a screen reader.

How different do the teams want their results to look?

A few colours can be settings. Pictures, two lines of text or a different layout per team need a way to hand the drawing of a result to the team.

Very different: one team shows products with pictures, one shows people, one shows plain text. Assume some pages will need other languages.

How many suggestions should show?

It sizes the list, whether a long list needs special handling, and how much a kept answer weighs.

No number is given. Say a short list is the default, cap it, and treat a very long list as its own problem.

The requirements menu

RequirementScopeWhy

Suggest results as someone types, with no submit button

Core

The whole point of the box. Without it, this is a search form.

Pick a suggestion with the mouse or the keyboard

Core

Mouse only fails keyboard and screen reader users, which the non-functional list rules out.

Show loading, no results and error states

Core

Answers take time and sometimes fail. A list that silently keeps old results looks current when it is not.

Let each team change how a result looks

Core

Other teams put the box on their own pages, each with its own kind of result.

Highlight the part of each result that matches the query

Optional

Take it if there is time: it helps people scan the list, and it raises a real question about inserting typed text into the page safely. Leave it if the core flow is not done.

Group results by kind, such as products and people

Optional

Take it if the teams search across several kinds of thing. It changes the list markup and how arrow keys move. Leave it for a single list of one kind.

Complete the top suggestion inline as someone types

Optional

Take it for a box where the first suggestion is nearly always right, such as a known list of places. Leave it for open search: it fills in text the person did not choose.

Pick more than one result, as tags

Optional

Take it if a team is filling a field with several values. It adds a list of chosen items and their own keys, which can crowd out the core in a short round.

Suggest something useful with no connection

Optional

Take it only if the interviewer asks: it needs data kept on the device and a plan for when it goes out of date.

How the server finds and ranks matches

Out of scope

A backend question. The box only needs a function that returns suggestions for a query.

The search results page after a pick

Out of scope

The box hands the pick to the team through its handler; what happens next is theirs.

Non-functional requirements

RequirementWhat it forces

Each keystroke answers within 200 milliseconds

The typed letter and the list’s state (loading, cleared) must paint at once, with nothing heavy in the key handler. The suggestions themselves cannot arrive that fast (see the estimate), so the 200 milliseconds covers the response to the key, not the network. Interaction to Next Paint measures exactly this: from the key press to the next frame on screen.

Usable with only a keyboard, and with a screen reader

The input follows the combobox pattern: it keeps focus, tells assistive technology when its list is showing and which option is active, and a live region that exists from the first render reads out how many results came back.

No request for an empty query or one under 2 characters

A check on the trimmed text before any request, and a list that clears at once when the box empties instead of after the next wait.

Works on phones and desktop browsers

Options large enough to tap, nothing that works only on hover, and a light render for each result, since phones have less processing power and every keystroke may redraw the list.

Estimates

QuantityWorkingResultWhat it sizes

Time from the last keystroke to suggestions on screen

Fast: a 200 ms wait + a 100 ms answer = 300 ms. Slow: a 350 ms wait + a 300 ms answer = 650 ms

About 300 ms with a fast server, 650 ms or more with a slow one

Both are past the 200 milliseconds a keystroke gets. So the keystroke budget is for painting the letter and the list’s state, never for the suggestions, which is why the list needs a loading state.

Requests for typing laptop with no wait

Queries of at least 2 characters: la, lap, lapt, lapto, laptop = 5 requests; with a wait after the last key = 1

5 requests without a wait, 1 with one

Why the wait comes first: without it the server gets five times the requests, and four of the answers are for text the person has already typed past.

Core entities

EntityWhat it holds

Query

What the person typed, and what the box sends.

Suggestion

One result the server returned, drawn as one option in the list.

Box state

Everything one box on the page knows right now; each box has its own.

Kept answer

A recent answer the box can show again without asking the server.

The API

EndpointNotes

fetchSuggestions(query, { signal }) → Promise of suggestions

Passed in by the team, with a default that calls the server. It takes an abort signal, so the box can cancel a request that a newer query made stale.

onSelect(suggestion)

Called when the person picks a suggestion, by click, tap or Enter. What happens next is the team’s.

renderSuggestion(suggestion, { active, query }) → element

Draws the inside of one option. The box keeps the option element itself, its role, id and keys, so a team changes the look and never the behaviour.

strings: { loading, noResults, error }

Every visible and announced string, with English defaults, so a page in another language passes its own.

GET /search?q={query} → { results }

The default data call. The query is URL-encoded; the response schema beyond a list of results, paging and the error body are not given.

The high-level design

propsevery keyresultsTeam pageSearch inputServerSuggestion list

Step 1 of 5

Input, request, list

Start with the simplest box that works: the team’s page renders the input, every change of the text asks the server, and the list draws whatever comes back. It shows the data flow end to end, and it already breaks: a request per key, a request for an empty box, and answers that can land in any order.

fetch, onSelect,render, stringstextunder 2characters:clearafter a pausehitmissabort the olderoneGET /searchwith a signalnewest answercountonSelectTeam pageSearch inputArrow keys,Enter, EscapeQuery controllerBox stateRecent answersRequest managerStale requestServerSuggestion listTeam renderfunctionLive region
The whole design

What breaks first

The deep dives

Every keystroke. Someone types laptop quickly, and the answer for lap lands after the answer for laptop. What does your box show, and what makes sure it is the list for laptop?

A concept that helps here: Debounce and request cancellationA concept that helps here: Core Web Vitals

Remembering results. Someone types a word, deletes it, and types the same word again a few seconds later. What does your box do the second time, and when would that be wrong?

A concept that helps here: Client caching and normalized stores

Keyboard and screen readers. Someone using only a keyboard and a screen reader types into the box and wants the third suggestion. Walk through each key they press and what they hear.

A concept that helps here: Accessible widgets

One box, many pages. Three teams use your box: one shows products with pictures, one shows people, one shows plain text. How does each get its own look without changing your box?

A concept that helps here: Designing a component APIA concept that helps here: List virtualization

Concepts this guide uses

Sources

  1. techinterview.org: Build a search-as-you-type input
  2. techinterview.org: Machine coding, autocomplete with debouncing
  3. W3C ARIA Authoring Practices Guide: Combobox pattern
  4. MDN: ARIA live regions
  5. MDN: AbortController
  6. MDN: AbortSignal
  7. Front End Interview Handbook: UI components
  8. MDN: HTTP caching
  9. web.dev: Web Vitals
  10. web.dev: Interaction to Next Paint (INP)
Sign in to report a mistake