Frontend School

Virtualized Product Catalog

Machine Coding
medium

Virtualized Product Catalog

Medium
React
Virtualization
Performance
UI

Overview

At Lenskart, our product catalog can grow to encompass thousands of eyewear products. Displaying such a large list efficiently is critical for a smooth user experience. A traditional approach of rendering all items simultaneously would lead to significant performance bottlenecks, especially on lower-end devices.

This problem challenges you to build a performant product listing page by implementing a virtualized list. You will render only the items currently visible in the viewport, plus a small buffer, ensuring a fluid scrolling experience even with a massive dataset.

What you need to do / Task

Your primary task is to implement a React component that displays a large list of Lenskart products using a fixed-height virtualization strategy. You are provided with mock product data and basic styling.

  1. Create a Virtualized List Component: Develop a React component (e.g., VirtualizedProductList) that takes an array of Product objects as a prop.
  2. Implement Virtualization Logic: This component should only render the Product items that are currently visible within its scrollable container, plus a small buffer of items above and below the viewport.
  3. Handle Scrolling: Efficiently listen to scroll events on the container and update the rendered items as the user scrolls.
  4. Position Items Correctly: Ensure that the visible items are correctly positioned within the scrollable area, maintaining the illusion that all items are present in the DOM.

UI/UX expectations

  • The product list should be contained within a fixed-height div with vertical scrolling enabled (overflow-y: auto).
  • Each product item should clearly display its imageUrl, name, and price as provided in the mock data.
  • Scrolling should be smooth and responsive, without any noticeable lag or jank, even when rapidly scrolling through thousands of items.
  • The overall layout should be clean and readable, using the provided basic CSS structure.

Acceptance criteria

  • Only a subset of the total TOTAL_PRODUCTS (e.g., 20-50 items, depending on viewport size) should be present in the DOM at any given time, regardless of the total number of products.
  • As the user scrolls down, new items should appear at the bottom of the viewport, and items scrolling out of view at the top should be removed from the DOM.
  • Conversely, scrolling up should bring previously removed items back into view.
  • The component must correctly display the name, price, and imageUrl for all visible products.
  • There should be no significant performance degradation or stuttering when scrolling through 10,000 products.
  • The fixed ITEM_HEIGHT constant must be correctly utilized for calculations.

Constraints

Constraints & assumptions

  • Fixed Item Height: Assume all product items have a fixed, consistent height (ITEM_HEIGHT is provided in App.tsx). You do not need to handle variable item heights.
  • Data Size: The mock data will contain TOTAL_PRODUCTS = 10000 items. Your solution should scale efficiently for this number.
  • Environment: React with TypeScript. You are free to use React hooks (useState, useEffect, useRef, useCallback, etc.).
  • Browser API: You can use standard browser APIs like HTMLElement.scrollTop, HTMLElement.clientHeight, etc.
  • No External Libraries: Do not use third-party virtualization libraries (e.g., react-window, react-virtualized). The goal is to implement the core logic yourself.

Out of scope

  • Fetching data from an API (mock data is provided).
  • Implementing infinite scrolling (loading more data as the user reaches the end).
  • Handling dynamic resizing of the container or items.
  • Complex filtering, sorting, or search functionalities.
  • Accessibility features beyond basic semantic HTML.

What interviewers evaluate

  • Correctness of Virtualization Logic: Accuracy in determining which items to render based on scroll position and viewport size.
  • Performance and Efficiency: The solution's ability to maintain smooth scrolling and minimal DOM operations with a large dataset.
  • Clean Component Architecture: Well-structured and maintainable React code, leveraging appropriate hooks and patterns.
  • Effective Scroll Handling: Proper management of scroll events to avoid performance issues (e.g., unnecessary re-renders).
  • Understanding of UI Performance: Demonstrates an understanding of why virtualization is needed and how to implement it effectively.

Loading editor...