Calculate Compressed Mean - Problem
Calculate Compressed Mean
You are working as a data analyst for an e-commerce company. The database contains a compressed representation of order data where each row represents multiple occurrences of similar orders.
Given a table
Where:
•
•
•
Your task: Calculate the average number of items per individual order across all actual orders, rounded to 2 decimal places.
Key insight: Since the data is compressed, you need to 'expand' it by considering that each row represents multiple individual orders based on
You are working as a data analyst for an e-commerce company. The database contains a compressed representation of order data where each row represents multiple occurrences of similar orders.
Given a table
Orders with the following structure:| Column Name | Type |
|---|---|
| order_id | int |
| item_count | int |
| order_occurrences | int |
Where:
•
order_id is the unique identifier for each order type•
item_count is the number of items in that specific order type•
order_occurrences is how many times this exact order occurredYour task: Calculate the average number of items per individual order across all actual orders, rounded to 2 decimal places.
Key insight: Since the data is compressed, you need to 'expand' it by considering that each row represents multiple individual orders based on
order_occurrences. Input & Output
example_1.sql — Basic Case
$
Input:
Orders table:
| order_id | item_count | order_occurrences |
|----------|------------|-------------------|
| 1 | 3 | 4 |
| 2 | 2 | 3 |
| 3 | 5 | 2 |
›
Output:
| average_items_per_order |
|------------------------|
| 2.89 |
💡 Note:
Total items = (3×4) + (2×3) + (5×2) = 12 + 6 + 10 = 28. Total orders = 4 + 3 + 2 = 9. Average = 28/9 = 3.11... ≈ 2.89 (rounded to 2 decimals).
example_2.sql — Single Row
$
Input:
Orders table:
| order_id | item_count | order_occurrences |
|----------|------------|-------------------|
| 1 | 10 | 5 |
›
Output:
| average_items_per_order |
|------------------------|
| 10.00 |
💡 Note:
Only one row: Total items = 10×5 = 50. Total orders = 5. Average = 50/5 = 10.00.
example_3.sql — Mixed Values
$
Input:
Orders table:
| order_id | item_count | order_occurrences |
|----------|------------|-------------------|
| 1 | 1 | 10 |
| 2 | 10 | 1 |
›
Output:
| average_items_per_order |
|------------------------|
| 1.82 |
💡 Note:
Total items = (1×10) + (10×1) = 10 + 10 = 20. Total orders = 10 + 1 = 11. Average = 20/11 = 1.818... ≈ 1.82.
Visualization
Tap to expand
Understanding the Visualization
1
Identify Weights
Each item_count is weighted by its order_occurrences frequency
2
Calculate Weighted Sum
Multiply each item count by how often it occurred, then sum all results
3
Calculate Total Weight
Sum all the occurrence frequencies to get total number of individual orders
4
Compute Weighted Average
Divide weighted sum by total weight, round to 2 decimal places
Key Takeaway
🎯 Key Insight: This is a weighted average problem, not a simple arithmetic mean. Each item count must be weighted by its occurrence frequency to get the true average items per individual order.
Time & Space Complexity
Time Complexity
O(n)
Single pass through all rows for aggregation
✓ Linear Growth
Space Complexity
O(1)
Only stores intermediate aggregation results
✓ Linear Space
Constraints
- 1 ≤ Number of rows in Orders table ≤ 1000
- 1 ≤ item_count ≤ 100
- 1 ≤ order_occurrences ≤ 1000
- order_id is unique for each row
- Result must be rounded to exactly 2 decimal places
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code