logo

Back to questions

Compressed Mode [Alibaba SQL Interview Question]

Medium

Given a table containing the item count for each order and the frequency of orders with that item count, write a query to determine the mode of the number of items purchased per order on Alibaba. If there are several item counts with the same frequency, you should sort them in ascending order.

Effective April 22nd, 2023, the problem statement and solution have been revised for enhanced clarity.

Table:

Column NameType
item_countinteger
order_occurrencesinteger

Example Input:

item_countorder_occurrences
1500
21000
3800
41000

Example Output:

mode
2
4

Explanation

Based on the example output, the value of 1000 corresponds to the highest frequency among all item counts. Specifically, both item counts of 2 and 4 have occurred 1000 times, making them tied for the most common number of occurrences.

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

PostgreSQL 14