Facebook Pixel

How to Build a Star Rating Component in an Interview

Star rating with hover, click, and half-star support. Here is how to build it in 90 minutes.

How to Build a Star Rating Component in an Interview

The star rating is a common machine coding question. Here is how to build it with hover, click, and half-star support.

Requirements

  • 5 stars displayed.
  • Hover highlights stars up to the hovered one.
  • Click locks the rating.
  • Half-star support (optional).
  • Keyboard accessible.
  • Reset on mouse leave (if not clicked).

Plan (5 minutes)

  1. HTML: 5 star elements.
  2. State: current rating, hover rating.
  3. Events: mouseover (hover), mouseleave (reset hover), click (lock).
  4. CSS: filled vs empty stars, transition.

Build Core (40 minutes)

HTML

<div class="star-rating" id="starRating"> <span class="star" data-value="1">&#9734;</span> <span class="star" data-value="2">&#9734;</span> <span class="star" data-value="3">&#9734;</span> <span class="star" data-value="4">&#9734;</span> <span class="star" data-value="5">&#9734;</span> </div> <p>Rating: <span id="ratingValue">0</span></p>

JavaScript

let rating = 0; let hoverRating = 0; const stars = document.querySelectorAll(".star"); const ratingValue = document.getElementById("ratingValue"); function updateStars() { stars.forEach((star, index) => { star.textContent = index < (hoverRating || rating) ? "★" : "☆"; }); ratingValue.textContent = hoverRating || rating; } stars.forEach((star, index) => { star.addEventListener("mouseover", () => { hoverRating = index + 1; updateStars(); }); star.addEventListener("mouseleave", () => { hoverRating = 0; updateStars(); }); star.addEventListener("click", () => { rating = index + 1; hoverRating = 0; updateStars(); }); });

CSS

.star-rating { font-size: 32px; cursor: pointer; } .star { color: #gold; padding: 2px; transition: color 0.2s; } .star:hover { transform: scale(1.2); }

Edge Cases (20 minutes)

  • Half-star support (split each star into two halves with separate hover areas).
  • Keyboard navigation (arrow keys to change, enter to select).
  • Reset button.
  • Readonly mode (display only, no interaction).
  • ARIA attributes (role="slider", aria-valuenow).

Half-Star Support

<span class="star-half" data-value="0.5">&#9734;</span> <span class="star-half" data-value="1">&#9734;</span> `` Use clip-path or two overlaid elements for half-star rendering. ### The Takeaway Build the star rating: HTML (5 star spans), JavaScript (hover to preview, click to lock, reset on leave), CSS (filled/empty stars, hover scale). Handle half-stars with split hover areas. Add keyboard support and ARIA. This tests event handling and state management.

Create 5 star elements. On mouseover, highlight up to the hovered star. On click, lock the rating. On mouseleave, reset the hover (keep the locked rating). Use Unicode stars (filled/empty). Add keyboard and ARIA support.

Split each star into two halves with separate hover/click areas. Use clip-path or two overlaid elements. The left half sets 0.5, the right half sets 1.0. This allows precision rating like 3.5 out of 5.

Half-star support, keyboard navigation (arrow keys, enter), reset button, readonly mode (display only), and ARIA attributes (role='slider', aria-valuenow, aria-valuemax). Also handle mouseleave to reset hover but keep locked rating.

Add tabindex='0' and role='slider'. Handle keydown: ArrowRight/Up to increase, ArrowLeft/Down to decrease, Enter to confirm. Update aria-valuenow. This makes the rating usable without a mouse.

Yes, reset the hover preview on mouseleave, but keep the clicked/locked rating. So if the user hovers 4 stars then moves away, the display goes back to the locked rating (e.g., 3 if they previously clicked 3).

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.