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
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Estimates
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- Every keystroke
- Remembering results
- Keyboard and screen readers
- One box, many pages
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
- Meta
- Instacart
- Atlassian
- Databricks
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
| Ask | Why it matters | A 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 |
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. |
Non-functional requirements
| Requirement | What 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
| Quantity | Working | Result | What 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
| Entity | What 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
| Endpoint | Notes |
|---|---|
| Passed in by the team, with a default that calls the server. It takes an abort |
| Called when the person picks a suggestion, by click, tap or Enter. What happens next is the team’s. |
| 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. |
| Every visible and announced string, with English defaults, so a page in another language passes its own. |
| 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
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.
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?
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?
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.
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?
Concepts this guide uses
Sources
- techinterview.org: Build a search-as-you-type input
- techinterview.org: Machine coding, autocomplete with debouncing
- W3C ARIA Authoring Practices Guide: Combobox pattern
- MDN: ARIA live regions
- MDN: AbortController
- MDN: AbortSignal
- Front End Interview Handbook: UI components
- MDN: HTTP caching
- web.dev: Web Vitals
- web.dev: Interaction to Next Paint (INP)