Hone logo
Hone
Problems

Group Sold Products By The Date

Imagine you are working for an e-commerce platform. A common requirement is to analyze sales data to understand product performance over time. This challenge asks you to process a list of product sales and aggregate them by their sale date, providing a clear overview of how many units of each product were sold on any given day.

Problem Description

You will be given a list of product sales records. Each record contains information about a product, the quantity sold, and the date of the sale. Your task is to group these sales records by the date they occurred. For each date, you need to list all the unique products sold on that day and the total quantity of each product sold.

Key Requirements:

  • Process a collection of sales records.
  • Group the sales by the sale_date.
  • For each sale_date, identify all product_ids sold.
  • For each product_id within a specific sale_date, calculate the total_quantity sold.
  • The output should be structured to show the sale_date, product_id, and total_quantity.

Expected Behavior:

The output should be a list of aggregated sales, ordered first by sale_date in ascending order, and then by product_id in ascending order.

Edge Cases to Consider:

  • No sales on a particular date: If there are no sales records for a specific date within the range of your data, that date should not appear in the output.
  • Multiple sales of the same product on the same day: These should be consolidated into a single entry for that product on that date with the sum of quantities.
  • Empty input list: If the input list of sales records is empty, the output should also be empty.

Examples

Example 1:

Input: [
  {"product_id": "P001", "quantity": 5, "sale_date": "2023-10-26"},
  {"product_id": "P002", "quantity": 2, "sale_date": "2023-10-26"},
  {"product_id": "P001", "quantity": 3, "sale_date": "2023-10-26"},
  {"product_id": "P003", "quantity": 1, "sale_date": "2023-10-27"},
  {"product_id": "P002", "quantity": 4, "sale_date": "2023-10-27"}
]
Output: [
  {"sale_date": "2023-10-26", "product_id": "P001", "total_quantity": 8},
  {"sale_date": "2023-10-26", "product_id": "P002", "total_quantity": 2},
  {"sale_date": "2023-10-27", "product_id": "P002", "total_quantity": 4},
  {"sale_date": "2023-10-27", "product_id": "P003", "total_quantity": 1}
]

Explanation:

On "2023-10-26", "P001" was sold twice (5 + 3 = 8 units) and "P002" was sold once (2 units). On "2023-10-27", "P002" was sold once (4 units) and "P003" was sold once (1 unit). The output is ordered by date and then by product ID.

Example 2:

Input: [
  {"product_id": "A1", "quantity": 10, "sale_date": "2024-01-15"},
  {"product_id": "B2", "quantity": 5, "sale_date": "2024-01-16"},
  {"product_id": "A1", "quantity": 5, "sale_date": "2024-01-15"}
]
Output: [
  {"sale_date": "2024-01-15", "product_id": "A1", "total_quantity": 15},
  {"sale_date": "2024-01-16", "product_id": "B2", "total_quantity": 5}
]

Explanation:

On "2024-01-15", product "A1" had two sales entries, totaling 15 units (10 + 5). On "2024-01-16", product "B2" was sold once for 5 units.

Example 3 (Empty Input):

Input: []
Output: []

Explanation:

An empty input list results in an empty output list.

Constraints

  • The number of sales records will be between 0 and 1000.
  • product_id will be a string and will not be empty.
  • quantity will be an integer and will be between 1 and 100.
  • sale_date will be a string in "YYYY-MM-DD" format.
  • The solution should be efficient enough to handle up to 1000 records within a reasonable time frame.

Notes

  • You'll need a data structure to store the aggregated results as you process the input. A map or dictionary where keys represent dates and values represent another map of product IDs to quantities is a good starting point.
  • Pay close attention to the sorting requirements for the final output.
  • Consider how you will handle the aggregation of quantities for the same product on the same day.
Loading editor...
plaintext