Practice Hub /
E-Commerce & ScaleMedium~30 min interview roundGoogleAmazonSwiggy

Autocomplete & Typeahead Search Bar (Google / Amazon)

Design a snappy typeahead search input combining debouncing, request cancellation (AbortController), an LRU cache, race-condition guards, and ARIA combobox navigation.

1. Interactive System Architecture Diagram

Click any architecture component below to inspect its exact responsibilities and trace connected data flows.

Keystrokes 0ms Lookup Debounced Fetch Hot Prefixes Search Input UI ARIA Combobox Query Controller Debounce + Abort LRU Client Cache Map + TTL Eviction Search API Trie / Elastic Prefix Edge Prefix Cache Top 1k Hot Queries
Selected ComponentSearch Input UI — ARIA Combobox
  • Sets `role="combobox"`, `aria-expanded`, `aria-activedescendant`, and `role="listbox"`.
  • Handles `ArrowUp`, `ArrowDown`, `Enter`, and `Escape` cleanly via keyboard.
  • Highlights matching substring tokens safely using JSX/Angular text nodes (never raw `innerHTML`!).

2. Interactive Trade-Off Simulator

Toggle between real architectural options to compare Pros, Cons, and exactly when to pick each choice in interviews.

Input Trigger Timing

How should typing trigger autocomplete network fetches cleanly?

Pros

  • Waits until user pauses briefly, cutting API requests by ~80% vs firing every keystroke.
  • Cancels slower prior requests if user resumes typing immediately.

Cons & Pitfalls

  • Adds ~220ms intentional wait on cache misses.
Interview Rule of Thumb: Pick Debounce + AbortController for all search boxes and filter inputs.
Client Caching Policy

How do you keep memory bounded when users perform dozens of searches?

Pros

  • Instant 0ms results on Backspace / repeat prefixes with strictly bounded memory.
  • Deleting & re-inserting in JS `Map` preserves recency in O(1) without extra library.

Cons & Pitfalls

  • Clears when tab closes (usually desired for autocomplete freshness).
Interview Rule of Thumb: Pick Bounded LRU Cache for typeahead, route prefetches & tooltip previews.

3. RADIO Framework Breakdown & Interview Checklist

Review each RADIO pillar cleanly and check off sections as you master them.

0 / 5 Sections Reviewed
R

Requirements & Scope

  • Functional: Top 8–10 prefix suggestions, bold matched text, keyboard nav, recent queries.
  • Non-Functional: Perceived response < 100ms, zero race conditions, full WCAG combobox accessibility.
A

Architecture Components

  • UI Combobox -> Debouncer -> LRU Cache Check -> Fetcher (`AbortController` + Seq ID).
  • Separate highlight renderer escapes regex characters cleanly to prevent XSS.
D

Client Data Model

  • `SearchState: { query, results[], activeIndex, isOpen, isLoading, error }`
  • `CacheEntry: { items: Suggestion[], cachedAt: number }`
I

Interface & API Contracts

  • `GET /api/v1/search/suggest?q=react&limit=8` -> `{ query, items: [{ id, label, category }] }`
  • Normalize `query.trim().toLowerCase()` before cache lookup & network request.
O

Performance & Optimizations

  • Keep previous visible suggestions dim/stale while loading new query (no jarring empty flash!).
  • Trigger speculative prefetch of top result metadata only when user hovers or presses ArrowDown.
  • Never execute `setState` cleanly if `signal.aborted === true`.