What is the best way to solve Five-Star Sellers question?

Third-party companies that sell their products on eBay.com are able to analyze the customer reviews for their products in real time. Imagine that Amazon is creating a category called "five-star sellers" that will only display products sold by companies whose average percentage of five-star reviews per-product is at or above a certain threshold. Given the number of five-star and total reviews for each product a company sells, as well as the threshold percentage, what is the  **minimum number of additional fivestar reviews**  the company needs to become a five-star seller?

For example, let's say there are 3 products (n = 3) where productRatings = [[4,4], [1,2], [3, 6]], and the percentage ratings Threshold = 77. The first number for each product in productRatings denotes the number of fivestar reviews, and the second denotes the number of total reviews. Here is how we can get the seller to reach the threshold with the minimum number of additional five-star reviews:

* Before we add more five-star reviews, the percentage for this seller is ((4 / 4) + (1/2) + (3/6))/3 = 66.66%
* If we add a five-star review to the second product, the percentage rises to ((4 / 4) + (2/3) +(3/6))/3 = 72.22%
* If we add another five-star review to the second product, the percentage rises to ((4 / 4) + (3/4) + (3/6))/3 = 75.00%
* If we add a five-star review to the third product, the percentage rises to ((4/4) + (3/4) + (4/7))/3 = 77.38%

At this point, the threshold of 77% has been met. Therefore,  **the answer is 3**  because that is the minimum number of additional five-star reviews the company needs to become a five-star seller.

**Function Description**

Complete the function fiveStarReviews in the editor below.

fiveStarReviews has the following parameters:

int productRatings[n][2]: a 2-dimensional array of integers where the ith element contains two values, the first one denoting fivestar[i] and the second denoting total[i]

int ratingsThreshold: the threshold percentage, which is the average percentage of five-star reviews the products need for the company to be considered a five-star seller

Returns:

int: the minimum number of additional five-star reviews the company needs to meet the threshold ratingsThreshold

**Constraints**

* 1<=n<=200
* 0 <= fivestar<total<=100
* 1<=ratingsThreshold<100
* The array productRatings contains only non-negative integers.

Homework?

What do you have so far? I think before we can help you determine the “best” way, we have to first determine “any” way and optimize from there. I don’t actually know the best way (or even how to determine what the best way would be), but I was able to write up some code that produces the same answer as the example, given the same sequence of productRatings

Like NobbZ, I suspect this is a homework question and we’re all willing to help here, but I don’t think anyone benefits from simply being provided a completed answer to a question like this. I think it’s in your own best interest to attempt to break down the problem into smaller parts and solve smaller parts of the problem before attempting to solve the whole solution.

The first step that I took was to identify the “shape” of the data involved in the problem. I defined the following two data types:

type productRating struct {
    // fiveStar is the count of the subset of the total ratings
    // that are five stars.
    fiveStar int

    // total count of ratings of a product
    total int
}

type productRatings []productRating

The second step I took was to identify a smaller question within the whole problem, specifically: “How do I calculate the five star percentage from a collection of product ratings?” The example showed the exact calculation, so I just wrote the code to perform that calculation.

Because I already know my data types, and because the calculation is performed on a collection of product ratings, it made sense to me to put the function that calculates the percentage of five star ratings for a collection of product ratings on the productRatings type:

func (prs productRatings) percentFiveStar() float32 {
    // ...
}

As you solve smaller parts of the problem with helper functions, you can eventually solve the problem as a whole with your helper functions. Good luck! Let us know if you have any more questions, but please be sure to provide what you’ve tried and we can help guide you in solving the problem yourself!

3 Likes