Question presented to candidate: "Build the photo carousel for our home page: one photo at a time, previous and next arrows that wrap around at the ends, dots to jump to any photo, and autoplay every three seconds. Autoplay must pause while the user is hovering or using the keyboard inside it, and there has to be a way to stop it. Make it keyboard accessible."
Requirements:
- Show one slide at a time from an array of images; a sliding CSS transition between slides.
- Previous / next buttons that wrap (last to first, first to last) and one dot per slide that jumps to it.
- Left / Right arrow keys (and Home / End) when the carousel has focus.
- Autoplay every 3 s; pauses on hover and on keyboard focus inside; a Play / Pause button stops it for good.
- Visible "2 / 5" status and correct ARIA for the carousel and its slides.
What a strong answer should cover:
- Index wrap-around with
((i % n) + n) % n, so-1becomes the last slide. - One source of truth (
index) and derived values (running = playing && !hovered && !focused) instead of duplicated flags. - Timer cleanup: a timeout or interval cleared when the slide changes, when paused, and on unmount; no stale closures.
- Accessibility:
aria-roledescription="carousel"and"slide", labels like "2 of 5", hidden slides markedaria-hidden, a pause control (WCAG 2.2.2), andaria-liveoff while rotating, polite when paused. - The transform-based track (
translateX(-index * 100%)) andprefers-reduced-motion.
Clarifying questions expected:
- "Should autoplay restart after the user leaves the carousel, or stay stopped once they interact?"
- "Do we need swipe on touch devices or an infinite (no rewind) animation?"
- "How many images, and should off-screen ones be lazy-loaded?"
Code / implementation expected: Yes. A working carousel component with arrows, dots, keyboard support, autoplay with pause, and cleanup of the timer.