Frontend School

Airbnb Destination Autocomplete Search

Machine Coding
medium

Airbnb Destination Autocomplete Search

Medium
Autocomplete Search
Frontend
React
TypeScript
UI/UX

Overview

At Airbnb, providing an intuitive and efficient search experience is paramount to helping guests find their perfect stay. A key component of this is the destination search bar, which guides users to valid and popular locations. This problem focuses on building an autocomplete search functionality for destinations, enhancing usability by suggesting options as the user types and reducing friction in the booking process. Your solution will directly impact how easily guests discover their next adventure on Airbnb.

What you need to do / Task

Your task is to build a React component that provides an autocomplete search experience for Airbnb destinations. You will be provided with a mock API function (mockFetchSuggestions) that simulates fetching destination data.

  1. Autocomplete Input: Create a text input field where users can type their desired destination.
  2. Fetch Suggestions: As the user types, call the mockFetchSuggestions API to retrieve relevant destination suggestions. Implement debouncing to limit the frequency of API calls (e.g., 300ms).
  3. Display Suggestions: Show the fetched suggestions in a dropdown list positioned directly below the input field. The dropdown should appear when the input is focused and there are suggestions, and disappear when a suggestion is selected or the input loses focus (and no suggestions are applicable).
  4. Highlight Matches: Within each suggestion, visually highlight (e.g., bold the text) the portion that matches the user's current input query.
  5. Selection Handling:
    • When a user clicks on a suggestion, the input field should be populated with the selected destination, the dropdown should close, and the onSelectDestination callback should be invoked with the selected value.
    • Implement keyboard navigation: Users should be able to navigate the suggestions list using the Up and Down arrow keys. Pressing Enter on a highlighted suggestion should select it, populate the input, and close the dropdown.

UI/UX expectations

  • The search input should always be visible.
  • The suggestions dropdown should appear dynamically below the input when a user types or focuses on the input and relevant suggestions are available.
  • Suggestions should be presented as a clear, vertical list. If there are many suggestions, the list should be scrollable.
  • The currently highlighted suggestion (via keyboard navigation) should have a distinct visual style (e.g., a background color change).
  • Clicking outside the autocomplete component should close the suggestions dropdown.
  • Ensure basic accessibility attributes (e.g., aria-label, aria-autocomplete, aria-controls, role, aria-selected) are used where appropriate for the input and suggestion list.

Acceptance criteria

  • Typing 'san' into the input field should, after a brief delay, display suggestions like 'San Francisco', 'Santa Monica', etc.
  • Subsequent typing (e.g., 'sanc') should update the suggestions list accordingly (e.g., only 'San Francisco').
  • API calls to mockFetchSuggestions are debounced, meaning rapid typing does not trigger an API call for every keystroke.
  • Clicking a suggestion populates the input with the selected value, clears the suggestions, and triggers the onSelectDestination callback.
  • Pressing the Down arrow key moves the highlight through suggestions; Up moves it backward.
  • Pressing Enter on a highlighted suggestion selects it and performs the same actions as a click.
  • The part of the suggestion text matching the input query is visually distinct (e.g., bold).
  • Clicking outside the autocomplete area closes the suggestions dropdown.

Constraints

Constraints & assumptions

  • You are working in a React environment with TypeScript.
  • The mockFetchSuggestions function simulates an asynchronous API call; you do not need to implement the backend or modify this function.
  • Assume the mockFetchSuggestions API returns pre-filtered and relevant results based on the query. Your task is to integrate it, not to implement the filtering logic itself.
  • The maximum number of suggestions to display at any time is handled by the mockFetchSuggestions function (currently 7).
  • Focus on functional correctness and a clean, maintainable React component structure.
  • Target completion time: ~45 minutes.

Out of scope

  • Complex error handling for API calls (e.g., displaying error messages).
  • Caching of API results.
  • Full form submission or integration with a global state management system beyond the component itself.
  • Advanced UI animations or custom scrollbars.

What interviewers evaluate

  • Correctness and Robustness: Does the autocomplete function as expected, handling various user inputs and edge cases (e.g., empty input, no suggestions)?
  • React Principles & Code Quality: Is the component well-structured, readable, and does it leverage React's features effectively (e.g., hooks, state management)?
  • Asynchronous Operations: Is debouncing correctly implemented, and are asynchronous API calls handled gracefully to prevent race conditions or unnecessary fetches?
  • UI/UX & Accessibility: Does the component provide a smooth user experience, including keyboard navigation and basic accessibility considerations?
  • Problem Solving: How effectively the candidate breaks down the problem and implements the required features within the given time.

Loading editor...