Orders With Maximum Quantity Above Average
This challenge requires you to process a list of customer orders and identify those that stand out. Specifically, you need to find all orders where the quantity ordered is greater than the average quantity of all orders. This is useful for identifying high-volume orders that might require special attention or analysis.
Problem Description
You will be given a list of orders, where each order is represented as an object or structure containing at least two key pieces of information: an order_id and a quantity. Your task is to:
- Calculate the average quantity of all orders in the provided list.
- Identify and return a new list containing only those orders whose
quantityis strictly greater than the calculated average quantity. - The returned list should maintain the original structure of the orders (i.e., each item in the output list should be an order object/structure with
order_idandquantity).
Key Requirements:
- Handle cases where the input list of orders might be empty.
- Ensure the average quantity calculation is performed correctly, even with fractional results.
- The comparison for "above average" should be strictly greater than (>).
Expected Behavior:
- If the input list is empty, an empty list should be returned.
- If no orders have a quantity greater than the average, an empty list should be returned.
Edge Cases:
- Empty Input: What happens if no orders are provided?
- All Quantities Equal: If all orders have the same quantity, the average will be that quantity, and no orders will be above average.
Examples
Example 1:
Input: [
{"order_id": "A101", "quantity": 5},
{"order_id": "A102", "quantity": 10},
{"order_id": "A103", "quantity": 3},
{"order_id": "A104", "quantity": 8}
]
Output: [
{"order_id": "A102", "quantity": 10},
{"order_id": "A104", "quantity": 8}
]
Explanation:
The total quantity is 5 + 10 + 3 + 8 = 26.
The number of orders is 4.
The average quantity is 26 / 4 = 6.5.
Orders with quantity > 6.5 are A102 (10) and A104 (8).
Example 2:
Input: [
{"order_id": "B201", "quantity": 15},
{"order_id": "B202", "quantity": 15},
{"order_id": "B203", "quantity": 15}
]
Output: []
Explanation:
The total quantity is 15 + 15 + 15 = 45.
The number of orders is 3.
The average quantity is 45 / 3 = 15.
No orders have a quantity strictly greater than 15.
Example 3:
Input: []
Output: []
Explanation:
The input list of orders is empty, so an empty list is returned.
Constraints
- The number of orders in the input list will be between 0 and 1000, inclusive.
- Each
order_idwill be a non-empty string. - Each
quantitywill be an integer between 1 and 100, inclusive. - The solution should aim for an efficient time complexity, ideally O(n), where n is the number of orders.
Notes
- When calculating the average, be mindful of floating-point precision.
- The problem asks for orders with quantities strictly greater than the average.
- Consider how you would represent an "order" in your chosen programming language (e.g., a dictionary, a class, a struct).