KatInterview

Top Three Categories by Average October Sale Value

SQL & DataAuto-graded · 12 test cases
MediumAuto-gradedSQL & Dataaggregationhash-mapgroup-by

Last updated

Filter records to one month, group by key, apply a minimum-count threshold, compute a rounded average, and return the top three groups, mirroring a SQL WHERE/GROUP BY/HAVING/ORDER BY/LIMIT pipeline.

Best Categories by Average Sale Value

An online marketplace keeps its transactions and its product categories as two lists of dictionaries, which stand in for database tables.

sales (each dictionary has these keys)

KeyTypeMeaning
sale_idintUnique id of the sale
category_idintRefers to a category_id in categories
seller_idintId of the seller
sale_datestrDate in YYYY-MM-DD format
revenuestr or numberRevenue of the sale in USD (convertible with float(...))

categories (each dictionary has these keys)

KeyTypeMeaning
category_idintUnique id of the category
category_namestrDisplay name of the category

Task

Write a function that finds the three categories with the highest average revenue per sale in October 2023.

  1. Consider only sales whose sale_date falls in October 2023 (dates from 2023-10-01 through 2023-10-31).
  2. Group these sales by category_id.
  3. Drop every category that has fewer than 10 October sales. A category with exactly 10 sales stays.
  4. For each remaining category, compute:
    • total_revenue: the sum of float(revenue) over its October sales, added up in the order the sales appear in the input, with no rounding.
    • average_revenue_per_sale: total_revenue divided by the number of its October sales, rounded to 2 decimal places with Python's built-in round(x, 2).
  5. Sort by average_revenue_per_sale (the rounded value) from highest to lowest and return at most the first 3 entries.

Output format

Return a list of dictionaries, each with exactly these keys:

  • category_name: the name looked up from categories.
  • total_revenue: a number. If the value is a whole number, give it as an int (for example 1500). Otherwise give the float.
  • average_revenue_per_sale: the rounded average, with the same rule (an int when it is a whole number, otherwise a float).

Tie-breaking and edge cases

  • If several categories have the same rounded average, they keep the order in which each category first appeared among the October sales in sales. Use a stable sort.
  • If fewer than 3 categories qualify, return only those. If none qualify, return an empty list.
  • Sales outside October 2023, including 30 September and 1 November, never count toward any total or count.
  • Every category_id in sales is guaranteed to exist in categories.

Constraints

  • 0 <= len(sales) <= 10^5
  • 0 <= len(categories) <= 10^4
  • sale_date is always a valid string in YYYY-MM-DD format.
  • revenue is always convertible to a float and is never missing.
  • Category ids in categories are unique.

Examples

Example 1

Input: top_categories_by_avg_sale([{'sale_id': 1, 'category_id': 1, 'seller_id': 7, 'sale_date': '2023-10-01', 'revenue': '100.00'}, {'sale_id': 2, 'category_id': 1, 'seller_id': 7, 'sale_date': '2023-10-02', 'revenue': '100.00'}, {'sale_id': 3, 'category_id': 1, 'seller_id': 7, 'sale_date': '2023-10-03', 'revenue': '100.00'}, {'sale_id': 4, 'category_id': 1, 'seller_id': 7, 'sale_date': '2023-10-04', 'revenue': '100.00'}, {'sale_id': 5, 'category_id': 1, 'seller_id': 7, 'sale_date': '2023-10-05', 'revenue': '100.00'}, {'sale_id': 6, 'category_id': 1, 'seller_id': 7, 'sale_date': '2023-10-06', 'revenue': '100.00'}, {'sale_id': 7, 'category_id': 1, 'seller_id': 7, 'sale_date': '2023-10-07', 'revenue': '100.00'}, {'sale_id': 8, 'category_id': 1, 'seller_id': 7, 'sale_date': '2023-10-08', 'revenue': '100.00'}, {'sale_id': 9, 'category_id': 1, 'seller_id': 7, 'sale_date': '2023-10-09', 'revenue': '100.00'}, {'sale_id': 10, 'category_id': 1, 'seller_id': 7, 'sale_date': '2023-10-10', 'revenue': '100.00'}, {'sale_id': 11, 'category_id': 1, 'seller_id': 7, 'sale_date': '2023-10-11', 'revenue': '100.00'}, {'sale_id': 12, 'category_id': 1, 'seller_id': 7, 'sale_date': '2023-10-12', 'revenue': '100.00'}, {'sale_id': 13, 'category_id': 1, 'seller_id': 7, 'sale_date': '2023-10-13', 'revenue': '100.00'}, {'sale_id': 14, 'category_id': 1, 'seller_id': 7, 'sale_date': '2023-10-14', 'revenue': '100.00'}, {'sale_id': 15, 'category_id': 1, 'seller_id': 7, 'sale_date': '2023-10-15', 'revenue': '100.00'}, {'sale_id': 16, 'category_id': 2, 'seller_id': 8, 'sale_date': '2023-10-01', 'revenue': '20.00'}, {'sale_id': 17, 'category_id': 2, 'seller_id': 8, 'sale_date': '2023-10-02', 'revenue': '20.00'}, {'sale_id': 18, 'category_id': 2, 'seller_id': 8, 'sale_date': '2023-10-03', 'revenue': '20.00'}, {'sale_id': 19, 'category_id': 2, 'seller_id': 8, 'sale_date': '2023-10-04', 'revenue': '20.00'}, {'sale_id': 20, 'category_id': 2, 'seller_id': 8, 'sale_date': '2023-10-05', 'revenue': '20.00'}], [{'category_id': 1, 'category_name': 'Electronics'}, {'category_id': 2, 'category_name': 'Books'}])

Output: [{'category_name': 'Electronics', 'total_revenue': 1500, 'average_revenue_per_sale': 100}]

Hints

Hint 1

Think of the work as a pipeline: filter rows by month, group by category, filter groups by count, compute the metric, sort, and cut to three.

Hint 2

Build a dictionary from category_id to category_name first, then make one pass over the sales, keeping a count and a running revenue sum for each category whose sale_date starts with '2023-10'.

Hint 3

Sort on the already-rounded average using a stable sort, and iterate categories in first-seen order so ties resolve correctly. Convert whole-valued floats to int before returning.