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 Orders with the following structure:

Column NameType
order_idint
item_countint
order_occurrencesint

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 occurred

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 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
🎯 Weighted Average VisualizationRestaurant Tip Analogy: Finding Average Tip Per TableCompressed Data$53 tables$82 tables$124 tables$151 tableSummary formatinstead of individualreceiptsExpand Mentally$5 × 3 = $15 total$8 × 2 = $16 total$12 × 4 = $48 total$15 × 1 = $15 totalTotal Tips: $94Total Tables: 10Weighted Average$94 ÷ 10 tables= $9.40 per tableRounded to 2 decimals💡 Key Insight: This is NOT a simple arithmetic mean!If we incorrectly used (5+8+12+15)/4 = $10, we'd ignore the frequency weights.The correct weighted approach gives $9.40 because lower tips occurred more often.
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

n
2n
Linear Growth
Space Complexity
O(1)

Only stores intermediate aggregation results

n
2n
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
Asked in
Amazon 35 Meta 28 Google 22 Microsoft 18
28.8K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen