Machine Coding · Lesson 3 of 12 · 4 min
Building a Static Component: Star Rating
This tests your grasp of props, component composition, and list rendering—foundational skills for any UI role.
In a machine coding interview, you'll often be asked to build a seemingly simple UI element. The goal isn't just to get something on the screen. It's to see how you structure your code, handle data flow, and think about reusability. The star rating component is a classic test of these fundamentals.
Break the UI into components
Your first move should be to break the problem down. A "star rating" isn't one thing; it's a container holding several instances of another thing: a star.
This leads to two components:
StarRating: The parent container. It knows theratingand thetotalStars. Its job is to manage the collection of stars.Star: A child component. It knows nothing about the overall rating. Its only job is to render a single star, either filled or empty, based on a prop it receives.
This separation is non-negotiable. An interviewer wants to see that you default to composition. It demonstrates you understand separation of concerns and how to build a reusable UI toolkit, not just a one-off feature.
Drive rendering with props
A static component's appearance is a pure function of its props. It doesn't manage its own state. For StarRating, the essential props are rating and totalStars.
Here is the implementation. Notice the clear, one-way data flow: App -> StarRating -> Star.
// Star.jsx
// This component only cares about one thing: am I filled or not?
const Star = ({ filled }) => {
const starStyle = {
color: filled ? '#FFD700' : '#ccc',
display: 'inline-block',
fontSize: '24px',
};
return (
<span style={starStyle}>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="currentColor"
width="1em"
height="1em"
>
<path d="M12 17.27L18.18 21l-1.64-7.03L22 9.24l-7.19-.61L12 2 9.19 8.63 2 9.24l5.46 4.73L5.82 21z" />
</svg>
</span>
);
};
// StarRating.jsx
// This component orchestrates the rendering of multiple Stars.
const StarRating = ({ rating, totalStars = 5 }) => {
return (
<div>
{Array.from({ length: totalStars }).map((_, index) => {
const starValue = index + 1;
return <Star key={index} filled={starValue <= rating} />;
})}
</div>
);
};
The trap: Hardcoding the number of stars or the rating inside the component. This makes it useless in any other context. Always derive display from props.
Another trap: Misusing the key prop. Here, using index as the key is acceptable because the list of stars is static: it will never be re-ordered, added to, or filtered. For dynamic lists, using the index as a key can lead to subtle bugs and performance issues. You can read more about this at /learn/react-list-keys.
Implement the core display logic
The most important line in this entire component is filled={starValue <= rating}. This is the core logic that translates a number (rating) into a visual representation.
The logic works by treating each star as having an ordinal value. The first star is 1, the second is 2, and so on. In the loop, index + 1 gives us this value. We then simply check if the star's value is less than or equal to the current rating.
This flow can be visualized directly.
An interviewer wants to see you write this loop and conditional logic cleanly. They're not looking for a clever one-liner, but for readable, correct code that handles the mapping from data to UI.
How to handle follow-up questions
A good interviewer will push you beyond the basic implementation. Here are the two most common follow-ups.
"How would you handle partial stars, for a rating like 3.5?"
This question tests your ability to think about trade-offs. There are two valid paths:
- The Simple Path: Round the rating.
Math.floor(rating)would show 3 filled stars for 3.5. This is simple, predictable, and often good enough. Acknowledge that you're losing visual precision. - The Complex Path: Implement a true partial fill. This requires changing the
Starcomponent. Instead of a booleanfilledprop, it would need afillPercentageprop (a number from 0 to 1). You can then use CSS to clip the filled star icon. Common techniques include usingclip-pathor layering two SVGs inside a container withoverflow: hidden.
Your answer should be to present both options and their trade-offs. Start by suggesting the simple path, then offer the more complex one as an enhancement if visual fidelity is critical. This shows you can prioritize and balance complexity with requirements.
"How would you make this accessible?"
This is a critical question, especially for senior roles. A visual-only component is a broken component.
- The root
<div>of theStarRatingshould have anaria-labelthat describes the rating clearly, likearia-label="Rating: 4 out of 5 stars". - The individual star SVGs are purely decorative once the container has a proper label. They should be hidden from screen readers with
aria-hidden="true". - This ensures that a screen reader user gets the concise, correct information ("4 out of 5 stars") instead of a confusing announcement like "star, star, star, star, empty star".
Try it
Build a static ProgressBar component. It should accept a percentage prop (a number from 0 to 100) and render a single visual bar filled to that percentage. This uses the exact same pattern of mapping a numeric prop to a visual style, but in a different shape. Aim to complete it in 15 minutes.
Next: Handling User Input: Form Elements.
Lock it in — apply this lesson under interview conditions.