Frontend School

Real-time Product Search Autocomplete

Machine Coding
medium

Real-time Product Search Autocomplete

Medium
React
TypeScript
Frontend
Autocomplete
Search
Debouncing
UI

Overview

In modern e-commerce and content platforms, a fast and responsive search experience is crucial for user engagement. An effective autocomplete feature guides users quickly to their desired products or content, reducing search abandonment and improving overall satisfaction. This problem challenges you to build a foundational autocomplete search component that provides real-time suggestions as a user types.

What you need to do / Task

Your goal is to implement a React component that provides real-time search suggestions.

  1. Search Input: Create an input field where users can type their search query.
  2. Dynamic Suggestions: As the user types, display a list of relevant suggestions below the input field.
  3. Filtering Logic: Suggestions should be filtered from a predefined list (mock data) based on whether they contain the user's input string (case-insensitive substring match).
  4. Debouncing: Implement a debounce mechanism for fetching suggestions. The suggestion fetching function should only be called after the user has paused typing for a short duration (e.g., 300ms).
  5. Highlighting: Within the displayed suggestions, highlight the portion of the text that matches the current search term.
  6. Keyboard Navigation:
    • Allow users to navigate through the suggestion list using the ArrowUp and ArrowDown keys.
    • The currently selected suggestion should have a distinct visual style.
    • Pressing Enter when a suggestion is selected should populate the input field with the selected suggestion's text and clear the suggestion list.
  7. Click Selection: Clicking on a suggestion should also populate the input field with its text and clear the suggestion list.

UI/UX expectations

  • The search input should be clearly visible.
  • Suggestions should appear in a dropdown-like list directly below the input field when available.
  • The currently highlighted (via keyboard navigation or hover) suggestion should have a distinct background color.
  • The matching parts of suggestions should be visually differentiated (e.g., bolded or different color).
  • The component should be accessible for keyboard users.

Acceptance criteria

  • Typing in the input field updates the displayed suggestions after a short delay.
  • Suggestions correctly filter based on case-insensitive substring match.
  • The fetchSuggestions function is debounced (e.g., console log shows calls only after a pause).
  • Matching parts of suggestions are visually highlighted.
  • ArrowUp and ArrowDown keys correctly navigate through suggestions, wrapping around the list.
  • Pressing Enter on a selected suggestion populates the input and clears suggestions.
  • Clicking a suggestion populates the input and clears suggestions.
  • No suggestions are shown when the input is empty or when there are no matches.

Constraints

Constraints & assumptions

  • Data Source: You will use a static, in-memory array of strings as your product data. There is no actual backend API call required; simulate it using Promise and setTimeout.
  • Environment: React with TypeScript.
  • Time Budget: Aim to complete this within ~45 minutes. Focus on core functionality and clean implementation over extensive styling.
  • Debounce Delay: Use a debounce delay of approximately 300ms.

Out of scope

  • Complex ranking or sorting algorithms for suggestions.
  • Server-side search or pagination.
  • Persistence of search history.
  • Extensive error handling or network request states (beyond a simple loading indicator if you choose to add one).
  • Full, production-grade accessibility beyond the specified keyboard navigation.

What interviewers evaluate

  • Functional Correctness: Does the autocomplete logic, filtering, debouncing, and highlighting work as specified?
  • Component Design: Is the React component well-structured, maintaining clear state separation and responsibilities?
  • TypeScript Usage: Is TypeScript used effectively for type safety and code clarity?
  • User Experience: Is the keyboard navigation intuitive and responsive?
  • Code Quality: Is the code readable, maintainable, and free of obvious performance pitfalls?

Loading editor...