Frontend School

Debounced Product Search Input

Machine Coding
easy

Debounced Product Search Input

Easy
Debounce
Search
React
Frontend
Machine Coding

Overview

Users on e-commerce platforms expect a smooth and responsive search experience. Directly querying a backend API on every keystroke can be inefficient and overload servers, especially with fast typers. To optimize this, we implement a debounced search input that delays the API call until the user pauses typing, significantly improving performance and resource utilization.

What you need to do / Task

Your goal is to build a functional search component that fetches and displays product suggestions in a debounced manner.

  1. Render a search input field: A standard HTML <input type="text"> element.
  2. Display search results: Below the input, render a list of product names that match the current search query.
  3. Implement debouncing: The actual search logic (simulated API call) should only execute after the user has stopped typing for a specific duration (e.g., 300ms).
  4. Show a loading indicator: While the debounced search is in progress (after the debounce delay but before results are fetched), display a "Loading..." message.
  5. Handle empty states: If no results are found for a query, display a "No products found." message. If the search term is empty, clear results and show a prompt like "Start typing...".

UI/UX expectations

  • The search input should be clearly visible.
  • Search results should appear as an unordered list below the input.
  • The loading indicator should replace the results list when active.
  • The "No products found." message or "Start typing..." prompt should replace the results list when applicable.
  • Basic styling is appreciated for readability, but complex design is not the focus.

Acceptance criteria

  • Typing in the input updates its value immediately.
  • The simulated API call (search logic) is triggered only after a pause in typing (e.g., 300ms).
  • A "Loading..." message is displayed while the debounced search is active.
  • Matching product names are rendered in a list below the input.
  • If no products match, "No products found." is displayed.
  • Clearing the input should clear the displayed results/messages and show the initial prompt.

Constraints

Constraints & assumptions

  • Environment: React functional components.
  • Debounce time: Use a debounce delay of 300ms.
  • Mock API: You are provided with a mockFetchProducts function that simulates an asynchronous API call, returning a filtered list of products after a short delay. Do not modify this function.
  • Data: The PRODUCTS array is provided and should be used as the source for filtering.
  • No external libraries: Implement the debouncing logic yourself using standard React hooks and JavaScript; do not use libraries like lodash.debounce.
  • Frontend only: Focus on the client-side interaction and logic.

Out of scope

  • Extensive styling or complex UI animations.
  • Accessibility beyond basic semantic HTML elements.
  • Backend implementation or integration with a real API.
  • Advanced error handling beyond what's implicitly handled by the mock API returning empty results.

What interviewers evaluate

  • Correct implementation of debouncing logic without external libraries.
  • Effective state management for search query, results, and loading status.
  • Clean and readable component structure.
  • Adherence to UI/UX and acceptance criteria.

Loading editor...