KatInterview

Client-Side Filtering and Sorting with Async Data

System DesignModel answer
MediumModel answerSystem Designreactfrontend-architecturestate-management

Last updated

Design a frontend component that fetches paginated data, applies client-side search and sort, and handles race conditions efficiently.

Problem: Interactive Data Table with Remote Pagination

You're building a user interface that displays a catalog of films. The data arrives from a backend endpoint in pages, and users need to search and reorder the results without leaving the current page.

Your Task

Design and implement (or describe the architecture for) a component that:

  1. Loads data from a paginated endpoint — The API is GET /api/movies?page={n} and returns:

    {
      "data": [
        {"id": 1, "title": "Inception", "year": 2010},
        {"id": 2, "title": "The Matrix", "year": 1999}
      ],
      "totalPages": 12
    }
    

    Each page contains up to 50 records. Handle loading states and request failures gracefully.

  2. Implement a search filter — Users type into a text box to filter the current page by title (case-insensitive substring match). The filter should feel instant but not trigger excessive re-renders or computation.

  3. Provide sort controls — Users can toggle between sorting by title (alphabetical) or release year (most recent first). Sorting applies to the filtered subset.

  4. Optimize for responsiveness — The interface should remain snappy even when users type quickly or switch pages rapidly.

Constraints

  • The component must handle edge cases: empty results, network errors, concurrent requests, and malformed data.
  • Assume React or a similar declarative framework.
  • Focus on clean separation between server state, UI state, and computed state.

Example Interaction

  1. User loads page 3 (shows 50 movies).
  2. User types "dark" in the search box.
  3. Component filters the 50 loaded movies to ~8 matches containing "dark" in the title.
  4. User switches sort to "year" — the 8 matches reorder by year, most recent first.
  5. User navigates to page 4 — the search and sort preferences persist, but the filter now applies to the new page's 50 movies.

What We're Evaluating

  • How you structure state to avoid bugs (stale data, race conditions, unnecessary re-renders).
  • Your approach to async data fetching and error handling.
  • Performance considerations for filtering and sorting.
  • Code clarity and separation of concerns.

Hints

Hint 1

Separate server state (the raw fetched data) from derived state (filtered and sorted results). Never store computed values in useState.

Hint 2

Debounce the search input to reduce computation, but let the input itself update immediately so typing feels responsive.

Hint 3

Use an AbortController to cancel in-flight requests when the user changes pages before the previous request completes.