Introduction

Hello, and welcome to today's lesson! Today, we are going to dive into the world of managing product reviews and applying data aggregation in practice. We will start with a relatively simple Starter Task to set up our base, and then gradually build up to a more complex solution involving data aggregation. Let's jump in!

Starter Task: Methods and Their Definitions

For our starter task, we will lay the foundation by implementing basic operations for managing product reviews. The Review struct encapsulates the details of a product review, consisting of the following fields:

  • std::string text — The textual content of the review.
  • int rating — The rating score of the review, ranging from 1 to 5.
  • bool flagged — A boolean indicating whether the review is flagged as inappropriate.

These are the methods we will need to implement in a ReviewManager class:

  • bool add_review(std::string product_id, std::string review_id, std::string review_text, int rating) — adds a review to the product specified by product_id. If a review with review_id already exists, it updates the existing review. Returns true if the review was added or updated successfully, false otherwise. For newly added items, the flagged attribute is set to false initially.

  • std::optional<Review> get_review(std::string product_id, std::string review_id) — returns the review details (text, rating, and flagged fields) for the review specified by review_id under the given product_id. If the review or product does not exist, returns std::nullopt.

  • bool delete_review(std::string product_id, std::string review_id) — deletes the review specified by review_id under the given product_id. Returns true if the review was deleted, false otherwise.

Starter Task Implementation

Let's look at the code that implements these functionalities:

This code establishes the foundational methods needed for managing product reviews within a ReviewManager class. The add_review method allows for adding a new review or updating an existing one, ensuring each review contains valid rating values between 1 and 5. The get_review method retrieves the review details for a specific product, including the review text and rating, returning std::nullopt if the product or review doesn't exist. The delete_review method facilitates the removal of a specific review, and if no reviews are left for a product, the product itself is removed from the product list. Together, these methods form the basic operations required to manage a collection of product reviews efficiently.

Now, let's extend this with new features.

New Task: Advanced Functions and Data Aggregation

With our basic review management system in place, we will now introduce new methods to handle more complex operations, such as flagging inappropriate reviews and aggregating review data for a specific product.

Here are the new methods we will add:

  • bool flag_review(const std::string& product_id, const std::string& review_id) — This method flags a specific review as inappropriate for a given product. Returns true if the review was successfully flagged, false otherwise.

  • std::optional<AggregatedData> aggregate_reviews(const std::string& product_id) — This method aggregates review data for a given product, providing statistics such as the total number of reviews, the number of flagged reviews, average rating, and review texts excluding flagged ones. If the product does not have any reviews or does not exist, returns std::nullopt.

Implementation, Step 1: Adding the 'flag_review' Method

First, let's add functionality to flag a review:

In this step, we are adding the flag_review method to our ReviewManager class. This method enables users to mark a specific review as inappropriate. It checks whether the product and review exist in the dataset, and if they do, it sets the flagged attribute of the review to true. This flagging mechanism is crucial for maintaining the quality and appropriateness of the reviews in the system.

Step 2: Adding the 'aggregate_reviews' Method

Next, we will implement the method to aggregate reviews:

In this step, the aggregate_reviews method is added to the ReviewManager class, along with the definition of an AggregatedData custom struct. The method aggregates review data for a given product by calculating various statistics, such as the total number of reviews, the number of flagged reviews, the average rating excluding flagged reviews, and a list of review texts that are not flagged. The method first ensures the product exists and contains reviews. It then iterates through the reviews to collect the necessary data, considering only non-flagged reviews for the average rating and review texts. If all reviews are flagged, the average rating defaults to zero. This aggregation provides a comprehensive overview of a product’s review status, useful for both users and administrators.

Conclusion

Great job! Today, you have learned how to manage product reviews and apply data aggregation in practice. We started with basic operations for adding, retrieving, and deleting reviews. Then, we extended our functionality to include flagging reviews and aggregating review data. This gradual build-up demonstrates how to enhance features incrementally and handle more complex data aggregation tasks.

Feel free to practice solving similar challenges to strengthen your skills further. Keep coding, and see you in the next lesson!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal