Back to questions

Compressed Median Alibaba SQL Interview Question

Compressed Median

Alibaba SQL Interview Question

You are trying to find the median number of items bought per order on Alibaba, rounded to 1 decimal point.

However, instead of doing analytics on all Alibaba orders, you have access to a summary table, which describes how many items were in an order, and the number of orders that had that many items.

Table:

Column NameType
item_countinteger
order_occurrencesinteger

Example Input:

item_countorder_occurrences
1500
21000
3800
41000

Example Output:

median
3.0

Explanation

The total orders in the field in this dataset is 3300, meaning that the median item count would be for the 1650th order (3300 / 2 = 1650).

If we compare this to the running sum of , we can see that the median item count is 3.

The dataset you are querying against may have different input & output - this is just an example!

Input

Output