Design a Product Rating and Review System

Quick Overview

Implement a product rating and review class that supports submitting, overwriting and deleting ratings, average ratings, the newest reviews and a top-k product list. It tests choosing data structures for constant-time updates, correct overwrite handling and deterministic ranking with tie-breaks.

Design a Product Rating and Review System

Company: Booking.com

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a rating and review system that lets users submit, update and delete ratings for products. Each rating is an integer from `1` to `5` and may include an optional text review. A user can have at most one rating per product. Submitting another rating for the same `(userId, productId)` pair overwrites the previous one: the old score and the old review text are both replaced, and the new submission counts as the user's latest. The system must support querying the average rating of a product, retrieving its most recent reviews, and listing the top-rated products ranked by average rating. When ranking products or reviews, ties are broken deterministically: top products by higher average rating, then by smaller `productId`; recent reviews are ordered from newest to oldest by submission order. Implement the following methods: - `submitRating(userId: int, productId: int, rating: int, review: str)`: Submit a rating (1-5) with an optional review for a product. Overwrites any prior rating by the same user for the same product. An empty `review` means the rating has no review text. - `updateRating(userId: int, productId: int, rating: int, review: str)`: Update a user's rating for a product. Behaves like `submitRating` if none exists. - `deleteRating(userId: int, productId: int)`: Delete the user's rating and its review for the given product. Deleting a rating that does not exist does nothing. - `getAverageRating(productId: int) -> float`: Return the average rating of the product rounded to 2 decimal places, or `0.0` if no ratings exist. - `getRecentReviews(productId: int, k: int) -> List[str]`: Return up to `k` most recent non-empty review texts for the product, ordered newest first. - `getTopProducts(k: int) -> List[int]`: Return up to `k` product IDs with the highest average rating, ties broken by smaller `productId`. Products with no ratings are not ranked. Example 1 shows an overwrite and a tie in the ranking: ```text Input: ["RatingReviewSystem", "submitRating", "submitRating", "submitRating", "submitRating", "submitRating", "getAverageRating", "updateRating", "getAverageRating", "getRecentReviews", "getTopProducts"] [[], [1, 10, 5, "Great"], [2, 10, 3, ""], [3, 10, 4, "Good value"], [1, 20, 4, "Solid"], [4, 30, 4, ""], [10], [1, 10, 2, "Broke after a week"], [10], [10, 5], [3]] Output: [null, null, null, null, null, null, 4.0, null, 3.0, ["Broke after a week", "Good value"], [20, 30, 10]] Explanation: system = RatingReviewSystem() system.submitRating(1, 10, 5, "Great") # product 10: [5] system.submitRating(2, 10, 3, "") # product 10: [5, 3], no review text system.submitRating(3, 10, 4, "Good value") # product 10: [5, 3, 4] system.submitRating(1, 20, 4, "Solid") # product 20: [4] system.submitRating(4, 30, 4, "") # product 30: [4] system.getAverageRating(10) # 12 / 3 = 4.0 system.updateRating(1, 10, 2, "Broke after a week") # user 1's 5 and "Great" are replaced system.getAverageRating(10) # (2 + 3 + 4) / 3 = 3.0 system.getRecentReviews(10, 5) # ["Broke after a week", "Good value"] system.getTopProducts(3) # 20 and 30 tie at 4.0, smaller id first: [20, 30, 10] ``` Example 2 shows a delete: ```text Input: ["RatingReviewSystem", "submitRating", "submitRating", "deleteRating", "getAverageRating", "getRecentReviews", "getTopProducts"] [[], [1, 50, 4, "Nice"], [2, 50, 2, "Meh"], [2, 50], [50], [50, 5], [1]] Output: [null, null, null, null, 4.0, ["Nice"], [50]] Explanation: system = RatingReviewSystem() # empty system system.submitRating(1, 50, 4, "Nice") # product 50: [4] system.submitRating(2, 50, 2, "Meh") # product 50: [4, 2] system.deleteRating(2, 50) # removes user 2's rating and review system.getAverageRating(50) # 4.0 (only user 1 remains) system.getRecentReviews(50, 5) # ["Nice"] system.getTopProducts(1) # [50] ``` ```hint Keep the average cheap Decide what to store per product so an average never needs a pass over its ratings. Then work out what an overwrite and a delete must subtract from that state. ``` ```hint Overwrites move reviews A resubmitted review must leave its old place in the list. Pick a per-product structure that can remove one user's entry quickly and still hand back the newest entries in order. ``` ### Constraints & Assumptions - `1 <= rating <= 5` - `1 <= userId, productId <= 10^6` - `0 <= len(review) <= 500` - At most `10^5` total operations. - `1 <= k <= 100` for `getRecentReviews` and `getTopProducts`. ### Clarifying Questions to Ask - Should `getTopProducts` rank by the exact average or by the rounded value that `getAverageRating` returns? The two can disagree on ties. - How should an average that falls exactly halfway, such as 2.125, be rounded? - What should happen on invalid input, such as a rating of 0 or 6: raise an error or ignore the call? - Will `getTopProducts` be called much more often than ratings change? That decides whether a scan per query is acceptable. ### What a Strong Answer Covers - The state kept per product and per `(userId, productId)` pair, and how every write keeps the running sum and count correct, including an overwrite that must not change the count. - How the review structure handles a resubmission (old text gone, new text newest) and a delete without scanning the product's whole review list. - Deterministic ranking in `getTopProducts`: higher average first, smaller `productId` on ties, unrated products excluded, and no floating-point surprises when comparing averages. - A stated rounding rule for `getAverageRating` and the `0.0` case for a product with no ratings. - Time and space complexity of each method, including the cost of `getTopProducts` under the stated operation limit. ### Follow-up Questions - `getTopProducts` is now called on every page load. How would you answer it in $O(k)$ without scanning all products? - A product with a single 5-star rating currently outranks one with 2,000 ratings averaging 4.8. How would you change the ranking? - How would you make the class safe when many threads submit and delete ratings at once? - Ratings must now persist in a relational database. What tables and indexes back each method, and how do you keep the per-product sum and count consistent with the ratings?

Overview: Implement a product rating and review class that supports submitting, overwriting and deleting ratings, average ratings, the newest reviews and a top-k product list. It tests choosing data structures for constant-time updates, correct overwrite handling and deterministic ranking with tie-breaks.

Booking.com logo
Booking.com
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a rating and review system that lets users submit, update and delete ratings for products. Each rating is an integer from 1 to 5 and may include an optional text review.

A user can have at most one rating per product. Submitting another rating for the same (userId, productId) pair overwrites the previous one: the old score and the old review text are both replaced, and the new submission counts as the user's latest. The system must support querying the average rating of a product, retrieving its most recent reviews, and listing the top-rated products ranked by average rating.

When ranking products or reviews, ties are broken deterministically: top products by higher average rating, then by smaller productId; recent reviews are ordered from newest to oldest by submission order.

Implement the following methods:

  • submitRating(userId: int, productId: int, rating: int, review: str) : Submit a rating (1-5) with an optional review for a product. Overwrites any prior rating by the same user for the same product. An empty review means the rating has no review text.
  • updateRating(userId: int, productId: int, rating: int, review: str) : Update a user's rating for a product. Behaves like submitRating if none exists.
  • deleteRating(userId: int, productId: int) : Delete the user's rating and its review for the given product. Deleting a rating that does not exist does nothing.
  • getAverageRating(productId: int) -> float : Return the average rating of the product rounded to 2 decimal places, or 0.0 if no ratings exist.
  • getRecentReviews(productId: int, k: int) -> List[str] : Return up to k most recent non-empty review texts for the product, ordered newest first.
  • getTopProducts(k: int) -> List[int] : Return up to k product IDs with the highest average rating, ties broken by smaller productId . Products with no ratings are not ranked.

Example 1 shows an overwrite and a tie in the ranking:

Input:
["RatingReviewSystem", "submitRating", "submitRating", "submitRating", "submitRating", "submitRating", "getAverageRating", "updateRating", "getAverageRating", "getRecentReviews", "getTopProducts"]
[[], [1, 10, 5, "Great"], [2, 10, 3, ""], [3, 10, 4, "Good value"], [1, 20, 4, "Solid"], [4, 30, 4, ""], [10], [1, 10, 2, "Broke after a week"], [10], [10, 5], [3]]

Output: [null, null, null, null, null, null, 4.0, null, 3.0, ["Broke after a week", "Good value"], [20, 30, 10]]

Explanation:
system = RatingReviewSystem()
system.submitRating(1, 10, 5, "Great")               # product 10: [5]
system.submitRating(2, 10, 3, "")                    # product 10: [5, 3], no review text
system.submitRating(3, 10, 4, "Good value")          # product 10: [5, 3, 4]
system.submitRating(1, 20, 4, "Solid")               # product 20: [4]
system.submitRating(4, 30, 4, "")                    # product 30: [4]
system.getAverageRating(10)                          # 12 / 3 = 4.0
system.updateRating(1, 10, 2, "Broke after a week")  # user 1's 5 and "Great" are replaced
system.getAverageRating(10)                          # (2 + 3 + 4) / 3 = 3.0
system.getRecentReviews(10, 5)                       # ["Broke after a week", "Good value"]
system.getTopProducts(3)                             # 20 and 30 tie at 4.0, smaller id first: [20, 30, 10]

Example 2 shows a delete:

Input:
["RatingReviewSystem", "submitRating", "submitRating", "deleteRating", "getAverageRating", "getRecentReviews", "getTopProducts"]
[[], [1, 50, 4, "Nice"], [2, 50, 2, "Meh"], [2, 50], [50], [50, 5], [1]]

Output: [null, null, null, null, 4.0, ["Nice"], [50]]

Explanation:
system = RatingReviewSystem()             # empty system
system.submitRating(1, 50, 4, "Nice")     # product 50: [4]
system.submitRating(2, 50, 2, "Meh")      # product 50: [4, 2]
system.deleteRating(2, 50)                # removes user 2's rating and review
system.getAverageRating(50)               # 4.0 (only user 1 remains)
system.getRecentReviews(50, 5)            # ["Nice"]
system.getTopProducts(1)                  # [50]

Constraints & Assumptions

  • 1 <= rating <= 5
  • 1 <= userId, productId <= 10^6
  • 0 <= len(review) <= 500
  • At most 10^5 total operations.
  • 1 <= k <= 100 for getRecentReviews and getTopProducts .

Clarifying Questions to Ask Guidance

  • Should getTopProducts rank by the exact average or by the rounded value that getAverageRating returns? The two can disagree on ties.
  • How should an average that falls exactly halfway, such as 2.125, be rounded?
  • What should happen on invalid input, such as a rating of 0 or 6: raise an error or ignore the call?
  • Will getTopProducts be called much more often than ratings change? That decides whether a scan per query is acceptable.

What a Strong Answer Covers Guidance

  • The state kept per product and per (userId, productId) pair, and how every write keeps the running sum and count correct, including an overwrite that must not change the count.
  • How the review structure handles a resubmission (old text gone, new text newest) and a delete without scanning the product's whole review list.
  • Deterministic ranking in getTopProducts : higher average first, smaller productId on ties, unrated products excluded, and no floating-point surprises when comparing averages.
  • A stated rounding rule for getAverageRating and the 0.0 case for a product with no ratings.
  • Time and space complexity of each method, including the cost of getTopProducts under the stated operation limit.

Follow-up Questions Guidance

  • getTopProducts is now called on every page load. How would you answer it in O(k)O(k) without scanning all products?
  • A product with a single 5-star rating currently outranks one with 2,000 ratings averaging 4.8. How would you change the ranking?
  • How would you make the class safe when many threads submit and delete ratings at once?
  • Ratings must now persist in a relational database. What tables and indexes back each method, and how do you keep the per-product sum and count consistent with the ratings?