Calculate Compressed Mean - Problem

Given a table Orders containing compressed order data, calculate the average number of items per order rounded to 2 decimal places.

The table contains:

  • order_id: Unique identifier for each order type
  • item_count: Number of items in this order type
  • order_occurrences: How many times this order type occurred

Since the data is compressed, you need to expand it mathematically to find the true average across all individual orders.

Table Schema

Orders
Column Name Type Description
order_id PK int Unique identifier for each order type
item_count int Number of items in this order type
order_occurrences int How many times this order type occurred
Primary Key: order_id
Note: Each row represents a compressed order type with its occurrence count

Input & Output

Example 1 — Basic Calculation
Input Table:
order_id item_count order_occurrences
1 10 5
2 6 4
3 8 3
Output:
average_items_per_order
8.17
💡 Note:

We have 3 order types: 5 orders with 10 items each (50 total), 4 orders with 6 items each (24 total), and 3 orders with 8 items each (24 total). Total items = 50 + 24 + 24 = 98. Total orders = 5 + 4 + 3 = 12. Average = 98/12 = 8.1666... ≈ 8.17

Example 2 — Single Order Type
Input Table:
order_id item_count order_occurrences
1 15 3
Output:
average_items_per_order
15
💡 Note:

With only one order type, all 3 orders have exactly 15 items each. Total items = 15 × 3 = 45. Total orders = 3. Average = 45/3 = 15.00

Example 3 — Mixed Order Sizes
Input Table:
order_id item_count order_occurrences
1 1 10
2 20 2
Output:
average_items_per_order
4.17
💡 Note:

We have 10 small orders (1 item each) and 2 large orders (20 items each). Total items = 1×10 + 20×2 = 50. Total orders = 10 + 2 = 12. Average = 50/12 = 4.1666... ≈ 4.17

Constraints

  • 1 ≤ order_id ≤ 1000
  • 1 ≤ item_count ≤ 1000
  • 1 ≤ order_occurrences ≤ 1000
  • All values are positive integers
  • Result should be rounded to exactly 2 decimal places

Visualization

Tap to expand
Calculate Compressed MeanCompressed Dataorder_iditemscount1105264Expanded5 orders10 items each4 orders6 items eachCalculateAverageResult8.17Average ItemsPer OrderRepresents 5+4=9 actual orderswith 10×5 + 6×4 = 74 total items
Understanding the Visualization
1
Input
Compressed order data with occurrences
2
Expand
Calculate total items per order type
3
Average
Weighted average across all orders
Key Takeaway
🎯 Key Insight: When data is compressed with occurrence counts, use weighted averages to find the true mean across all individual records
Asked in
Amazon 23 Microsoft 18 Apple 15
28.5K Views
Medium Frequency
~8 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