Back

Performant Image Slider

medium

Streak

0 days

Progress

0%

Submitted

0

Performant Image Slider

React40 minmediumFree

Prompt

Interview answer

Keep only currentIndex and pause state, derive the visible image, wrap previous and next with modulo arithmetic, clean up the autoplay interval in useEffect, and make buttons, dots, alt text, focus, and reduced-motion behavior accessible.

Build ImageSlider({ images = [] }) in 40 minutes. Images have unique id, src, and meaningful alt. The supplied fixture uses local data URLs so no external image service is needed.

  • Show one image with a slide counter, wrapping Previous/Next buttons, and direct slide-picker buttons.
  • Rotation starts stopped. Start rotation advances one slide every 3 seconds; Stop rotation cancels it.
  • Hover temporarily suspends rotation. Keyboard focus entering from outside and manual navigation stop rotation until explicitly restarted.
  • Empty input shows an empty state. One image hides navigation, pickers, and rotation controls and creates no timer.
  • Clear timers on stop and unmount. Keep meaningful image alternatives and visible keyboard focus.

The image list is static during the core task. Swipes, transitions, preloading, image failures, and dynamic replacement are follow-ups.

Learn and continue

Prerequisite: Effect dependencies and cleanup. Work through the React Machine Coding Foundations path to practice related skills in order.

Requirements

  • →Previous from slide 1 wraps to the last; Next returns to slide 1
  • →Each picker shows its matching image, alt text, counter, and current marker
  • →Rotation starts stopped; Start advances after 3000ms and Stop cancels it
  • →Hover suspends rotation and leaving resumes only when playing is still true
  • →Keyboard entry and manual navigation stop rotation until Start is pressed
  • →Empty and single-image inputs have no navigation or active interval
  • →Unmount while rotating removes the interval

Starter packet

What the interviewer handed you

Complete the supplied image-slider component using the prompt contract. The shell and fixtures are provided; TODOs identify the behavior to implement.

Manual acceptance checks

  • Previous from slide 1 wraps to the last; Next returns to slide 1
  • Each picker shows its matching image, alt text, counter, and current marker
  • Rotation starts stopped; Start advances after 3000ms and Stop cancels it
  • Hover suspends rotation and leaving resumes only when playing is still true
  • Keyboard entry and manual navigation stop rotation until Start is pressed
  • Empty and single-image inputs have no navigation or active interval
  • Unmount while rotating removes the interval

Likely follow-ups

  • • How do you avoid a stale closure in an interval?
  • • What changes for reduced-motion users?
Example
Loading preview...
For the best coding experience, we recommend using a desktop device.
Preparing Sandbox...
Premium interview report

What interviewers score in this build

Use this before reading the code. It tells you what to say, what to test, and where machine-coding candidates usually lose points.

Interview signals

  • Core behavior: Previous from slide 1 wraps to the last; Next returns to slide 1
  • State and identity: The image index selects content; playing and hover determine whether a single interval may run.
  • Accessibility: Use the documented native keyboard behavior, meaningful names, and visible focus.
  • Edge cases: Demonstrate: Empty input, Single image, Wraparound, Hover during rotation, Keyboard entry then focus exit, Unmount with an active interval.

Time checkpoints

  1. 1

    0:00: Read the contract and walk through the fixture.

  2. 2

    5:00: Define state, identity, and keyboard behavior.

  3. 3

    30:00: Finish core interactions and lifecycle cleanup.

  4. 4

    35:00: Run the acceptance checks and edge cases.

Edge-case checklist

Empty input
Single image
Wraparound
Hover during rotation
Keyboard entry then focus exit
Unmount with an active interval

Common mistakes

  • Hard-coding a global image array inside a reusable component
  • Modulo by zero
  • Mismatched 3000ms prompt and 4000ms solution
  • Restarting rotation automatically after keyboard entry
  • aria-selected on ordinary picker buttons
SolutionRead-only · Live Preview

Technical Explanation

Separate selection from rotation

The current index chooses an image. A playing flag records the user's request to rotate; a hovered flag temporarily blocks that request. Derive rotating from those flags and image count. Starting stopped avoids unexpected movement and needs no special reduced-motion branch.

Wrap safely

Next is (index + 1) % count and Previous is (index - 1 + count) % count. Guard empty input before reading an image or taking a modulo. With one image, omit controls that cannot do useful work.

Let one effect own the interval

When rotating is true, create one 3000ms interval. Use a functional state updater so the callback advances from the latest index. Return clearInterval from the effect so stopping, hovering, and unmounting remove scheduled work. A browser may delay callbacks; 3 seconds is a requested delay, not a real-time guarantee.

Give users control

Place Start/Stop rotation first. Entering the carousel with keyboard focus stops rotation. Moving between its controls must not immediately undo an explicit Start click, so inspect relatedTarget to detect entry from outside. Previous, Next, and picker activation stop rotation too. Hover suspends rotation without erasing the playing preference.

Use button semantics

Picker buttons use aria-current for the current slide. aria-selected belongs to selectable widget roles and is not appropriate on these ordinary buttons. Native buttons already support Enter and Space, so there is no global arrow-key handler. Keep live announcements off while rotating and polite for manual changes.

Dry run

Previous on slide 1 reaches slide 3. Start rotation, advance the timer by 3000ms, then hover: no further advance occurs. Focus entering from outside stops rotation; leaving focus does not restart it. Empty and single-image inputs create no interval.

Interview Criteria

Core behavior

Previous from slide 1 wraps to the last; Next returns to slide 1

State and identity

The image index selects content; playing and hover determine whether a single interval may run.

Accessibility

Use the documented native keyboard behavior, meaningful names, and visible focus.

Edge cases

Demonstrate: Empty input, Single image, Wraparound, Hover during rotation, Keyboard entry then focus exit, Unmount with an active interval.

Reasoning

Explain the state model and the tradeoff behind the interaction contract.

Time Checkpoints

0:00

0:00: Read the contract and walk through the fixture.

5:00

5:00: Define state, identity, and keyboard behavior.

30:00

30:00: Finish core interactions and lifecycle cleanup.

35:00

35:00: Run the acceptance checks and edge cases.

40:00

40:00: Explain tradeoffs and follow-up scope.

Streak

0 days

Last active: Sign in to track

Progress

0%

0/0 solved

Submitted

0

Solutions pushed to review history.

Interview answer

How to build a React image slider in an interview

Keep only currentIndex and pause state, derive the visible image, wrap previous and next with modulo arithmetic, clean up the autoplay interval in useEffect, and make buttons, dots, alt text, focus, and reduced-motion behavior accessible.

  • Clarify empty, single-image, wraparound, autoplay, hover, focus, and keyboard behavior first.
  • Use functional state updates so rapid clicks and interval callbacks never read a stale index.
  • Reserve the frame with aspect-ratio and preload adjacent images to avoid layout shift and flicker.
  • Explain interval cleanup, negative modulo, meaningful alt text, and prefers-reduced-motion.
  • Handle image load failures
  • Design an image carousel
  • React machine coding questions
  • Search and render images

More Machine Coding

View all Machine Coding →
Machine CodingMedium

React Tic-Tac-Toe Engine

Machine CodingMedium

API Search and Render Image

Machine CodingEasy

Accessible React Tabs

Machine CodingMedium

Recursive File Explorer

Machine CodingEasy

Stopwatch

Machine CodingEasy

Tooltip

Related hubs

  • All machine coding
  • Frontend machine coding guide
  • React machine coding questions
  • UI coding interview questions
  • React UI interview questions
  • Amazon interview
  • Google interview
  • Meta interview

FAQ

Why do interviews ask for an Image Slider?

It tests timed or click-driven UI state, control buttons, edge wrapping, and keeping transitions predictable without layout thrash.

What details separate a strong solution?

Clear current-index state, disabled or wrapping controls at ends, keyboard support, and cleanup if autoplay is involved.

Related practice?

Stopwatch, traffic light, and carousel-adjacent modal/tooltip patterns.