Frontend School

Autocomplete Product Search

Machine Coding
medium

Autocomplete Product Search

Medium
Frontend
React
Autocomplete
Search
Debounce
Keyboard Navigation

Overview

As Paytm expands its e-commerce offerings, a seamless product search experience is crucial for user engagement and conversion. Autocomplete functionality significantly enhances user experience by providing instant, relevant suggestions as they type, reducing typing effort and guiding them to desired products faster. This problem focuses on building a robust and performant autocomplete search component.

What you need to do / Task

Your task is to implement an autocomplete search component for Paytm's product catalog. You will be provided with a mock fetchSuggestions function that simulates an API call to retrieve product suggestions based on a user's input.

  1. Build a Search Input Component: Create a React component that includes an input field for search queries.
  2. Fetch & Display Suggestions: As the user types, call the fetchSuggestions function with the current input value and display the returned suggestions in a dropdown list below the input field.
  3. Implement Debouncing: To prevent excessive API calls, implement a debouncing mechanism for the suggestion fetch logic. A delay of 300ms is recommended.
  4. Handle Loading State: Display a visual indicator (e.g., a spinner) while suggestions are being fetched.
  5. Keyboard Navigation: Enable users to navigate through the suggestions using ArrowUp and ArrowDown keys. Pressing Enter on a highlighted suggestion should select it and populate the input field. Escape should close the suggestions dropdown.
  6. Click Selection: Allow users to select a suggestion by clicking on it, which should populate the input field and close the dropdown.
  7. Empty State: Clearly indicate when no suggestions are found for a given query.

UI/UX expectations

  • Search Input: A clear, single-line text input field.
  • Suggestions Dropdown: A list of suggestions appearing directly below the input field. Each suggestion should be a clickable item.
  • Highlighting: The currently active (hovered or keyboard-navigated) suggestion should be visually highlighted.
  • Loading Indicator: A small spinner or text indicating that suggestions are being loaded.
  • No Results: A message like "No results found for 'query'" when the API returns an empty list for a non-empty query.
  • Focus Management: Suggestions should appear when the input is focused and there's a query, and disappear when the input loses focus (unless a suggestion is being clicked).

Acceptance criteria

  • Typing in the input field triggers suggestion fetching.
  • Suggestions appear in a dropdown after a brief delay (debouncing).
  • The loading spinner is visible during API calls and disappears afterward.
  • Suggestions are filtered correctly based on the input query (case-insensitive partial match).
  • Keyboard ArrowUp/ArrowDown navigates suggestions, highlighting the active one.
  • Pressing Enter on an active suggestion selects it and populates the input.
  • Clicking a suggestion selects it and populates the input.
  • Pressing Escape hides the suggestions dropdown.
  • Suggestions disappear when the input field is cleared or loses focus.
  • A message is displayed if no suggestions are found for a non-empty query.

Constraints

Constraints & assumptions

  • You must use React (functional components and hooks).
  • No external UI component libraries (e.g., Material UI, Ant Design) are allowed; basic HTML/CSS is expected.
  • The provided fetchSuggestions function simulates an asynchronous API call with a 300ms delay. Do not modify its implementation.
  • The maximum number of suggestions returned by fetchSuggestions is small (e.g., up to 20-30 items).
  • The search should be case-insensitive.
  • The debouncing delay should be approximately 300ms.

Out of scope

  • Advanced search features like fuzzy matching, spell correction, or personalized suggestions.
  • Complex routing or deep linking to product pages.
  • Server-side rendering or complex state management libraries (Redux, Zustand, etc.).
  • Accessibility features beyond basic keyboard navigation.

What interviewers evaluate

  • React Fundamentals: Correct use of state, props, hooks (useState, useEffect, useRef, useCallback).
  • Component Design: Modularity, separation of concerns, and clean component structure.
  • Logic Implementation: Correctness of debouncing, filtering, and keyboard navigation logic.
  • User Experience: Responsiveness, visual feedback (loading, active item), and intuitive interactions.
  • Code Quality: Readability, maintainability, and adherence to best practices.

Loading editor...