Product Sales Analysis II
This challenge focuses on analyzing product sales data to understand which products are performing best. You'll need to aggregate sales figures per product and then identify those that have achieved a certain sales threshold. This is a common task in business intelligence, helping to inform inventory management, marketing strategies, and product development decisions.
Problem Description
You are given a dataset of product sales transactions. Each transaction includes a product_id and the quantity sold. Your task is to:
- Calculate the total quantity sold for each unique product.
- Identify products where the total quantity sold is greater than or equal to a specified threshold.
- Return a list of these high-performing product IDs.
Key Requirements:
- Group sales by
product_id. - Sum the
quantityfor eachproduct_id. - Filter these aggregated results to include only products with a total quantity sold meeting or exceeding the threshold.
- The output should be a list of
product_ids.
Expected Behavior:
The function should accept a list of sales transactions and a threshold value. It should return a list of unique product_ids that meet the sales criteria. The order of the output list does not matter.
Edge Cases to Consider:
- No sales data: If the input list of transactions is empty, the output should be an empty list.
- No products meet the threshold: If no products achieve the sales threshold, the output should be an empty list.
- Duplicate product entries: The aggregation logic should correctly handle multiple entries for the same
product_id. - Zero quantity sales: Transactions with a quantity of 0 should be handled correctly (they contribute 0 to the total).
Examples
Example 1:
Input Transactions:
[
{ "product_id": 101, "quantity": 5 },
{ "product_id": 102, "quantity": 10 },
{ "product_id": 101, "quantity": 3 },
{ "product_id": 103, "quantity": 2 },
{ "product_id": 102, "quantity": 7 }
]
Sales Threshold: 15
Output:
[102]
Explanation:
Product 101: Total quantity = 5 + 3 = 8. (Less than 15)
Product 102: Total quantity = 10 + 7 = 17. (Greater than or equal to 15)
Product 103: Total quantity = 2. (Less than 15)
Only product 102 meets the threshold.
Example 2:
Input Transactions:
[
{ "product_id": 201, "quantity": 20 },
{ "product_id": 202, "quantity": 5 },
{ "product_id": 201, "quantity": 15 },
{ "product_id": 203, "quantity": 30 },
{ "product_id": 202, "quantity": 0 }
]
Sales Threshold: 25
Output:
[201, 203]
Explanation:
Product 201: Total quantity = 20 + 15 = 35. (Greater than or equal to 25)
Product 202: Total quantity = 5 + 0 = 5. (Less than 25)
Product 203: Total quantity = 30. (Greater than or equal to 25)
Products 201 and 203 meet the threshold.
Example 3 (Edge Case - No sales data):
Input Transactions:
[]
Sales Threshold: 10
Output:
[]
Explanation:
An empty input list results in an empty output list.
Constraints
- The number of transactions can range from 0 to 100,000.
product_idwill be an integer.quantitywill be a non-negative integer.- The
Sales Thresholdwill be a non-negative integer. - The solution should be reasonably efficient for the given number of transactions.
Notes
- You will need a data structure to store the aggregated quantities for each product. A hash map (or dictionary) is a suitable choice for this.
- Consider how you will iterate through the input transactions and update the aggregated counts.
- After aggregation, you'll need to iterate through the aggregated data to apply the filtering condition.
- The problem statement uses pseudocode for clarity; implement the logic in your chosen programming language.