Home
← All questions

Star rating component | Frontend Machine Coding

EasyPremiumMachine Coding

Asked at Amazon · Flipkart · Google

Frontend Machine Coding interview practice: Build an interactive star rating with hover preview, keyboard support, and half-star optional.

Problem

Build a reusable star rating widget used in product reviews.

Requirements

  • Display 5 stars; click to set rating (1–5).
  • Hover preview before click.
  • Keyboard: arrow keys adjust rating, Enter confirms.
  • Emit onChange(rating) to parent.
  • (Bonus) read-only mode, half-star increments, ARIA (role="slider").

Hints / Approach

  • Track rating (committed) and hover (preview) separately.
  • Render stars as buttons with aria-label.
  • Use onMouseEnter/onMouseLeave on container for hover state.
Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium