Frontend School

Build a Basic Image Carousel Component

Machine Coding
easy

Build a Basic Image Carousel Component

Easy
Frontend
React
UI
Carousel
Machine Coding

Overview

Image carousels are fundamental UI components in many web applications, from e-commerce product pages to personal portfolios. They allow users to browse multiple images in a compact and interactive manner. For this problem, you will implement a simplified image carousel that displays a series of images and allows users to navigate through them. This exercise evaluates your ability to manage component state, handle user interactions, and render dynamic UI elements.

What you need to do / Task

Your primary goal is to build a functional ImageCarousel React component that displays a collection of images.

  1. Display Images: Render a single image from a provided list at a time.
  2. Navigation Controls: Implement "Previous" and "Next" buttons to cycle through the images.
  3. Index Indicator: Show the current image's index relative to the total number of images (e.g., "1 of 5").
  4. Wrap Around: When navigating past the last image, the carousel should loop back to the first image. Similarly, navigating before the first image should loop to the last image.

UI/UX expectations

  • The currently displayed image should be clearly visible and centered within its container.
  • "Previous" and "Next" buttons should be easily clickable and positioned appropriately relative to the image (e.g., on either side or below).
  • The index indicator should be legible, indicating the current image number and the total count (e.g., "Image 3 of 7").
  • No advanced styling or animations are required, but the basic layout should be clean and functional.

Acceptance criteria

  • The ImageCarousel component renders without errors.
  • Initial render displays the first image in the provided list.
  • Clicking "Next" advances to the subsequent image.
  • Clicking "Previous" moves to the preceding image.
  • Clicking "Next" on the last image displays the first image.
  • Clicking "Previous" on the first image displays the last image.
  • The index indicator correctly updates to reflect the current image (e.g., "2 of 5" when the second image is shown).
  • All images from the provided data are accessible via navigation.

Constraints

Constraints & assumptions

  • You will be working within a React environment.
  • The images will be provided as an array of objects, each with an id, src, and alt property.
  • The carousel should function purely with JavaScript/TypeScript and React state; no external carousel libraries are allowed.
  • Focus on functional components and React Hooks for state management.
  • The maximum number of images will be small (e.g., 5-10).

Out of scope

  • Keyboard navigation (arrow keys).
  • Autoplay functionality.
  • Thumbnail navigation or dot indicators.
  • Advanced animations or transitions between images.
  • Loading states or error handling for images.
  • Responsiveness for different screen sizes beyond basic CSS.

What interviewers evaluate

  • State Management: Effective use of useState to manage the active image index.
  • Event Handling: Correct implementation of click handlers for navigation buttons.
  • Conditional Rendering & Logic: Ability to display the correct image and handle wrap-around logic.
  • Component Structure: Organization of the React component, including props and JSX structure.
  • Basic UI/UX: Meeting the fundamental visual and interactive requirements for a carousel.

Loading editor...