Frontend School

Instant Product Search Autocomplete

Machine Coding
medium

Instant Product Search Autocomplete

Medium
Autocomplete Search
Frontend
React
Debouncing
UI/UX

Overview

Instant search autocomplete is a critical feature for any e-commerce platform like Paytm. It significantly enhances user experience by reducing typing effort, guiding users to relevant products faster, and minimizing search abandonment. For an L4 engineer, building a robust and responsive autocomplete component demonstrates proficiency in frontend architecture, state management, and user interaction design.

What you need to do / Task

Your task is to build a React component that provides instant search suggestions for a predefined list of products. The component should be interactive and responsive.

  1. Search Input: Render a text input field where users can type their search queries.
  2. Dynamic Suggestions: As the user types, display a dropdown list of matching product suggestions below the input field.
  3. Filtering Logic: Suggestions should be filtered from a mock dataset. The filtering should be case-insensitive and match if the input string is a substring of any product name.
  4. Debouncing: Implement debouncing for the search logic (simulating an API call). Suggestions should not update on every keystroke, but only after a short delay (e.g., 300ms) of user inactivity.
  5. Keyboard Navigation: Allow users to navigate through the displayed suggestions using the Up and Down arrow keys. The currently highlighted suggestion should be visually distinct.
  6. Suggestion Selection: When a user clicks on a suggestion or presses Enter while a suggestion is highlighted, the search input field should be updated with the selected suggestion's text, and the suggestion list should be cleared.
  7. Clear Suggestions: The suggestion list should disappear when the input is empty or when the input loses focus (unless a suggestion is being selected).

UI/UX expectations

  • The search input should be clearly visible.
  • Suggestions should appear as a list directly below the input field.
  • The currently highlighted suggestion (via keyboard navigation or mouse hover) should have a distinct background color.
  • Basic styling for readability and a clean appearance is expected. Focus on functionality first.

Acceptance criteria

  • Typing "lap" into the input should display suggestions like "Laptop", "Apple Laptop", "Gaming Laptop" (assuming these are in the dataset).
  • Typing quickly should demonstrate debouncing; suggestions appear after a brief pause, not immediately with each character.
  • Pressing Down arrow key repeatedly cycles through suggestions, highlighting each one in turn.
  • Pressing Up arrow key repeatedly cycles through suggestions in reverse.
  • Clicking a suggestion or pressing Enter on a highlighted suggestion populates the input field and clears the suggestion list.
  • Deleting all text from the input clears the suggestion list.
  • Clicking outside the search component (blurring the input) should clear the suggestion list.

Constraints

Constraints & assumptions

  • You must use React for building the component.
  • A mock dataset of product names will be provided. You do not need to fetch data from a real API; simulate it using setTimeout within a function.
  • The debounce time should be set to 300ms.
  • Display a maximum of 5-7 suggestions at a time. If there are more matches, only show the top N.
  • Focus on core functionality. Styling should be minimal but functional.
  • The environment is a modern browser supporting ES6+ and React hooks.

Out of scope

  • Building a backend API for search.
  • Advanced fuzzy matching or complex search ranking algorithms.
  • Accessibility features beyond basic keyboard navigation.
  • Performance optimizations for extremely large datasets (assume a reasonable mock dataset size).

What interviewers evaluate

  • Correctness and Completeness: Does the component meet all functional requirements?
  • Code Structure and Readability: Is the code well-organized, clean, and easy to understand? Are React principles applied correctly?
  • State Management: Is component state managed efficiently using React hooks?
  • User Experience: Is the debouncing implemented effectively? Is keyboard navigation intuitive?
  • Problem Solving: How well are edge cases handled (e.g., no suggestions, empty input, blur event)?

Loading editor...