Frontend School

Autocomplete Search with Priority-Based Sorting

Machine Coding
hard

Autocomplete Search with Priority-Based Sorting

Hard
UI
React
Autocomplete
Search
Debounce
Accessibility

Overview

Snapchat's Lens Explorer allows users to discover a vast array of augmented reality lenses. A critical component of this experience is a fast and intuitive search bar. For this problem, you will build an autocomplete search component that provides real-time, intelligently sorted suggestions as the user types, ensuring a smooth discovery process for our users.

What you need to do / Task

Your goal is to implement a robust and user-friendly autocomplete search component in React. You will be provided with a mock API and data. Your implementation must satisfy the following:

  1. Search Input: Create a text input field where users can type their search queries.
  2. Debounced API Calls: Implement a debouncing mechanism for the search input. API calls to fetch suggestions should only be made after the user has paused typing for a short duration (e.g., 300ms).
  3. Suggestion Filtering & Sorting: Once suggestions are fetched, they must be filtered and sorted according to specific rules:
    • Filtering: Suggestions should match the search query case-insensitively (substring match).
    • Sorting Hierarchy:
      1. Exact Matches: Suggestions whose name is an exact, case-insensitive match for the query. These appear first, sorted by priority (descending).
      2. Starts With: Suggestions whose name starts with the query (case-insensitive), but are not exact matches. These appear next, sorted by priority (descending).
      3. Contains: Suggestions whose name contains the query (case-insensitive), but are not "exact" or "starts with" matches. These appear last, sorted by priority (descending).
    • Display a maximum of 10 suggestions at any time.
  4. Loading State: Display a clear loading indicator while suggestions are being fetched.
  5. No Results State: Clearly indicate when no suggestions are found for the current query.
  6. Keyboard Navigation: Implement keyboard navigation for the suggestion list:
    • ArrowUp/ArrowDown: Navigate through the suggestions.
    • Enter: Select the currently highlighted suggestion. This should populate the search input with the selected suggestion's name.
  7. Click Selection: Clicking a suggestion should also populate the search input with its name.

UI/UX expectations

  • The search input should be a standard text field.
  • Suggestions should appear in a dropdown list below the input field when available.
  • The currently highlighted suggestion (via hover or keyboard navigation) should have a distinct visual style.
  • When a suggestion is selected (by Enter or click), the dropdown should close, and the input field should display the selected suggestion's name.
  • Accessibility: Ensure appropriate ARIA attributes are used for the search input and suggestion list to enhance usability for assistive technologies.

Acceptance criteria

  • Typing in the search input triggers debounced calls to fetchSuggestions.
  • Suggestions appear as a dropdown list below the input, filtered and sorted according to the specified rules.
  • Only up to 10 suggestions are displayed.
  • A loading spinner or text is visible during API calls.
  • "No results found" message is displayed when no matches exist for a non-empty query.
  • ArrowUp and ArrowDown keys correctly navigate through suggestions, wrapping around at the ends.
  • Enter key selects the highlighted suggestion, updates the input, and closes the dropdown.
  • Clicking a suggestion selects it, updates the input, and closes the dropdown.
  • The currently active/highlighted suggestion is visually distinct.
  • The component is accessible with relevant ARIA roles and attributes (e.g., aria-autocomplete, aria-controls, aria-activedescendant).

Constraints

Constraints & assumptions

  • You are provided with a mock fetchSuggestions API function that simulates network latency (200-300ms).
  • The mock data set (MOCK_LENSES) contains approximately 100 items, each with an id, name, and priority (integer).
  • All filtering and sorting logic must be implemented client-side after the mock API returns the full dataset (for simplicity, the mock API returns all data, your code handles filtering/sorting).
  • The priority field is an integer; higher values indicate higher priority.
  • Focus on functional correctness, clean code, and user experience; extensive styling is not required beyond basic layout and visual feedback for interaction states.

Out of scope

  • Persistence of search history or recent searches.
  • Advanced error handling beyond displaying loading/no results states (e.g., network errors).
  • Full-page routing or integration with a backend service beyond the provided mock API.

What interviewers evaluate

  • Architectural Design: How well the component is structured, separation of concerns, and use of React hooks.
  • Algorithmic Correctness: Accuracy of the debouncing, filtering, and multi-level sorting logic.
  • User Experience: Responsiveness, keyboard navigation, and clear feedback for loading/empty states.
  • Code Quality: Readability, maintainability, and efficient use of resources (e.g., avoiding unnecessary re-renders).

Loading editor...