Design a Product Rating System
⭐ What is a product rating system? It is the part of an online store that lets customers give a product 1 to 5 stars and, if they want, write a review. The product page shows the average ("4.3 out of 5") and lists the reviews in an order the shopper picks: newest, highest, lowest or most helpful. The home page shows the best-rated products.
The request often comes as "ratings and reviews", "product reviews with averages" or "sort reviews". They are one design: a rating is the stars, a review is a rating with text, and the averages and sort orders are views over the same records. We cover all three parts.
It is an easy problem with two traps that interviewers like: keeping the average correct when people change or delete their rating, and comparing averages without floating-point surprises.
Requirements
"Design a rating and review system for an e-commerce site. Users rate products from 1 to 5 stars and can write reviews. Show the average rating and let shoppers sort reviews."
Here is what we ask, and what each answer decides.
Clarifying Questions
You: Can a user rate the same product more than once?
Interviewer: No, one rating per user per product. If they rate again, it replaces their old rating. They can also delete it.
You: Is the text required?
Interviewer: No. Many people only click stars. Those count in the average, but only ratings with text show up in the review list.
You: How is the average shown?
Interviewer: Rounded to one decimal, like 4.3. Round half up, so 4.25 shows as 4.3. A product with no ratings shows 0.0.
You: Which sort orders do shoppers get?
Interviewer: Newest, highest stars, lowest stars, and most helpful. Ties go to the newer review, then by user id so the order is stable.
You: What does "helpful" mean exactly?
Interviewer: Other users can mark a review as helpful, once each, and not their own. If the author edits the review, the old votes are cleared, because they were for the old text.
You: And the best-rated list on the home page?
Interviewer: Top k products by exact average. If two products tie, the one with more ratings wins, then product id. Products with no ratings are not listed.
You: How do we get the time?
Interviewer: Pass a timestamp into each call.
The last product question hides the second trap. Two products both rounded to 4.5 may not really be equal, so the ranking must use the exact average, not the displayed one.
💡 Interview tip: Ask what happens when a user changes their mind. "Rate again replaces" and "delete" are the two operations that break a naive running average, and asking about them early shows you already see the problem.
Final Requirements
- A user rates a product with 1 to 5 stars and optional text. Other star values are rejected.
- Each user has at most one rating per product. Rating again replaces it fully (stars, text, time) and clears its helpful votes. A user can delete their rating.
- The average is shown rounded half up to one decimal;
0.0with no ratings. - Written reviews (ratings with text) can be listed sorted by newest, highest, lowest or most helpful. Ties: newer first, then user id.
- A user can mark someone else's written review as helpful, once.
- The top
kproducts are ranked by exact average, then number of ratings, then product id. Unrated products are left out. - Sort orders are likely to grow (for example "verified buyers first"), so adding one should not touch the rest of the system.
Out of scope
- Moderation, spam detection and reporting reviews.
- Checking that the user bought the product.
- Photos, replies to reviews, and Q&A.
- Storage and many servers (we come back to scale in Extensibility).