Question presented to candidate: "On our product page we show the average rating as stars, and signed-in users can leave their own rating. Build a reusable star rating component that works for both: a read-only display that can show 3.6 stars, and an interactive version with hover preview that also works with the keyboard."
Requirements:
- Five stars by default (
maxis configurable). - Interactive mode: hovering previews a rating without committing it; clicking commits; clicking the current rating again clears it to 0.
- Keyboard: the stars behave like a radio group. Tab reaches one star, ArrowRight / ArrowUp raise the rating, ArrowLeft / ArrowDown lower it, Home and End jump to 1 and
max. - A caption shows the preview or the committed rating in words ("4 of 5, Good").
- Read-only mode renders fractional values (3.6 fills the fourth star 60 percent) and is announced as a single image ("Rated 3.6 out of 5").
- The parent owns the value (a controlled component).
What a strong answer should cover:
- Two pieces of state with different owners: the committed
value(parent) and the transienthover(component), rendered ashover || value. - Partial fill with two stacked glyphs, the top one clipped by
widthandoverflow: hidden. - ARIA:
role="radiogroup"withrole="radio"andaria-checkedper star, a rovingtabindex, androle="img"with a label for the read-only display. - Keyboard handling that moves focus together with the selection.
- Controlled API:
value+onChangein React,v-modelin Vue,[(value)]in Angular.
Clarifying questions expected:
- "Do we need half-star input, or only half-star display?"
- "Can a user clear their rating, and how?"
- "Is the rating saved immediately or submitted with a form?"
Code / implementation expected: Yes. A working component with both modes, hover preview, click and keyboard rating, and correct ARIA roles.