Frontend School

Debounced Product Search Component

Machine Coding
medium

Debounced Product Search Component

Medium
Debouncing
Search
React
Frontend
Machine Coding

Overview

As a leading e-commerce and financial technology company, Paytm handles millions of user interactions daily. A key feature in our product discovery is the search functionality. To provide a smooth user experience while efficiently managing backend resources, it's crucial to implement a search feature that is responsive but doesn't spam our servers with every single keystroke. This problem challenges you to build a debounced search component for a hypothetical Paytm Mall product catalog.

What you need to do / Task

Your goal is to build a React component that implements a debounced search functionality.

  1. Search Input: Create an input field where users can type their search queries.
  2. Debouncing Logic: Implement debouncing such that the fetchProducts API call is only made after a specified delay (e.g., 300ms) of user inactivity. Rapid consecutive typing should only trigger the API call once, after the user pauses.
  3. Display Results: Show the fetched product names in a list below the search input.
  4. Loading State: Display a "Loading..." message while the API call is in progress.
  5. No Results State: Display a "No results found." message if the API returns an empty list for a given search term.

UI/UX expectations

  • The component should consist of a search input element and a div or ul to display the search results.
  • When the user types, the input value should update immediately.
  • A "Loading..." indicator should appear below the input field while the simulated API call is ongoing.
  • Search results should be displayed as a simple list of product names.
  • If no results are found, a "No results found." message should be shown.
  • Styling can be minimal; focus on functionality and clear presentation of states.

Acceptance criteria

  • Typing a character in the search input does not immediately trigger the fetchProducts mock API.
  • An API call is initiated only after 300ms of no further input from the user.
  • If the user types "apple" character by character rapidly, only one API call for "apple" should be made after they stop typing for 300ms.
  • The component correctly displays product names returned by the mock API.
  • A "Loading..." message is visible between the time a debounced API call is triggered and when its results are received.
  • If the API returns an empty array, "No results found." is displayed.
  • Clearing the input field should clear the results and stop any pending API calls.

Constraints

Constraints & assumptions

  • Frontend Framework: React.js
  • Debounce Delay: Use a fixed delay of 300ms.
  • Mock API: A simulated fetchProducts function will be provided, which returns a Promise resolving with mock product data after a delay. Do not modify this mock API function.
  • Maximum Results: The mock API will return a maximum of 10 results.
  • Environment: Standard browser environment.
  • Data Structure: Products will be objects with at least a name property.

Out of scope

  • Extensive styling or use of UI component libraries.
  • Client-side routing.
  • Error handling beyond a simple console log or a basic message.
  • Pagination or infinite scrolling for results.
  • Advanced accessibility features beyond basic semantic HTML.

What interviewers evaluate

  • Correct Debounce Implementation: The primary focus is on correctly implementing the debouncing logic without race conditions.
  • React State Management: Effective use of useState and useEffect for managing component state (search term, results, loading).
  • Code Quality: Clean, readable, and maintainable code adhering to React best practices.
  • User Feedback: Proper display of loading and no-results states for a good user experience.

Loading editor...