KatInterview

Build a Type-Ahead Catalog Lookup with Cancellation and Caching

Data Structure DesignModel answer
MediumModel answerData Structure Designfrontendreactdebounce

Last updated

Build an accessible type-ahead lookup that delays requests, discards outdated responses, remembers past queries, supports keyboard control, and copes with errors.

Task

Build a type-ahead lookup box for an online store catalog. As the shopper types, the component asks a backend for matching items and shows at most 10 of them in a dropdown. It has to stay responsive and correct even when network latency varies from request to request.

You may use React (a component or a custom hook), or plain TypeScript/JavaScript.

Interface

interface Suggestion {
  id: string;
  title: string;
  category: string;
}

interface SearchBoxProps {
  fetchSuggestions: (query: string, signal: AbortSignal) => Promise<Suggestion[]>;
  onSelect: (item: Suggestion) => void;
  debounceDelayMs?: number; // default 300
  maxResults?: number;      // default 10
}

Required behavior

  1. Delayed requests. Only call the backend once the user has paused typing for debounceDelayMs (300 ms by default). Every new keystroke restarts the wait.
  2. Outdated responses. A slow response for an old query must never overwrite the results of a newer query. Cancel the old request, ignore its result, or both.
  3. In-memory cache. Keep results of recent queries so that repeating a query (for example after backspacing) is served instantly with no network call.
  4. Keyboard control. ArrowDown / ArrowUp move the highlighted suggestion, Enter picks the highlighted one, and Escape closes the dropdown.
  5. Accessibility. Follow the WAI-ARIA combobox pattern: role="combobox", aria-autocomplete="list", aria-expanded, and aria-activedescendant pointing to the highlighted option's DOM id.
  6. Visible states. Show distinct loading, empty-result, and error states.

Example flow

  • The user types s, then shoes within 200 ms. The first timer is discarded; 300 ms after the last keystroke one request goes out with an AbortSignal, and a loading indicator is shown until it returns.
  • The user presses ArrowDown. The first suggestion (index 0) is highlighted and aria-activedescendant references its id.
  • The user presses Enter. onSelect(item) fires, the dropdown closes, and the input shows the chosen title.

Edge cases to handle

  • Empty or whitespace-only input clears the suggestions and sends no request.
  • Typing apple, backspacing to app, then typing apple again must show the apple results immediately from the cache, with no new call.
  • A network failure must not erase or alter what the user has typed.

What to deliver

Explain your design, then provide code. Discuss complexity and the trade-offs you made (for example, how you handle cache size and selection side effects).

Hints

Hint 1

Treat the query text as the single source of truth. Each change should invalidate the previous timer and the previous in-flight request, then decide: blank, cached, or schedule a new fetch.

Hint 2

In React, a useEffect that creates one timer and one AbortController per run, and tears both down in its cleanup, gives you debouncing, cancellation, and unmount safety with one mechanism. Still check whether the controller was aborted after awaiting, in case the backend ignores the signal.

Hint 3

Selecting an item changes the input text, which would normally trigger another fetch. Think about how to suppress exactly that one fetch without swallowing the user's next keystroke. Also consider normalizing cache keys and only caching successful responses.