KatInterview

Category Revenue and Fulfillment Report from Three Tables

Arrays & HashingAuto-graded · 17 test cases
EasyAuto-gradedArrays & Hashinghash-mapaggregationjoin

Last updated

Join order, product and category records, group by category name, and compute total revenue, mean revenue per row and the share of completed rows, using hash maps.

Category Revenue and Fulfillment Report

A delivery marketplace stores its data in three lists of dictionaries:

  • orders: each entry has order_id, product_id, quantity, unit_price and status.
  • products: each entry has product_id and category_id.
  • categories: each entry has category_id and category_name.

Write a function that links these tables and produces one summary record per category name.

Joining

Treat the link as an inner join. An order row is used only if its product_id appears in products and that product's category_id appears in categories. Any other order row is ignored completely.

If the same product_id appears more than once in products, the last entry in the list wins. The same rule applies to a repeated category_id in categories.

Grouping

Group the surviving order rows by category_name (not by category_id). If two different category ids share a name, their rows belong to the same group.

Metrics for each group

Every surviving order row counts as one "order line", whatever its status.

  • total_sales: the sum of quantity * unit_price over all rows in the group.
  • average_sales_per_order: total_sales divided by the number of rows in the group.
  • completion_rate: the number of rows whose status is exactly "completed" (case-sensitive) divided by the number of rows in the group.

Rows that are not completed still contribute to total_sales and to the row count. The status only influences completion_rate.

Output

Return a list of dictionaries, one per group, each with the keys category_name, total_sales, average_sales_per_order and completion_rate. Sort the list by category_name in ascending order. Categories with no surviving order rows must not appear. If no rows survive, return an empty list.

Constraints

  • 0 <= len(orders) <= 10^5
  • 0 <= len(products), len(categories) <= 10^5
  • quantity and unit_price are non-negative numbers (integers or floats).
  • status is a string.
  • Category names are strings and can be compared with the usual ordering.

Examples

Example 1

Input: summarize_category_sales([{'order_id': 1, 'product_id': 10, 'quantity': 2, 'unit_price': 5, 'status': 'completed'}, {'order_id': 2, 'product_id': 11, 'quantity': 1, 'unit_price': 8, 'status': 'cancelled'}, {'order_id': 3, 'product_id': 10, 'quantity': 3, 'unit_price': 5, 'status': 'completed'}], [{'product_id': 10, 'category_id': 1}, {'product_id': 11, 'category_id': 1}], [{'category_id': 1, 'category_name': 'Meals'}])

Output: [{'category_name': 'Meals', 'total_sales': 33, 'average_sales_per_order': 11, 'completion_rate': 0.6666666666666666}]

Example 2

Input: summarize_category_sales([{'order_id': 1, 'product_id': 1, 'quantity': 1, 'unit_price': 12.5, 'status': 'completed'}, {'order_id': 2, 'product_id': 2, 'quantity': 2, 'unit_price': 3, 'status': 'pending'}], [{'product_id': 1, 'category_id': 7}, {'product_id': 2, 'category_id': 8}], [{'category_id': 7, 'category_name': 'Drinks'}, {'category_id': 8, 'category_name': 'Sides'}])

Output: [{'category_name': 'Drinks', 'total_sales': 12.5, 'average_sales_per_order': 12.5, 'completion_rate': 1}, {'category_name': 'Sides', 'total_sales': 6, 'average_sales_per_order': 6, 'completion_rate': 0}]

Hints

Hint 1

Build two dictionaries first: one from product id to category id and one from category id to category name. Then each order can be resolved in constant time.

Hint 2

Do not materialize a merged table. Keep, per category name, a running row count, a running sales total and a running count of completed rows.

Hint 3

Skip an order if either lookup fails. After the pass, derive both ratios by dividing by the row count, which is at least 1 for every group that exists, and sort by name.