KatInterview

Reconcile Orders with Product Catalog via Inner Join

SQL & DataAuto-graded · 13 test cases
MediumAuto-gradedSQL & Datadata manipulationjoinsaggregation

Last updated

Tests the ability to join two lists of dicts (orders + products) on a key, aggregate per customer, and return a cleaned, sorted summary.

Reconcile Orders with Product Catalog

You are given two tables represented as lists of dictionaries.

orders — each entry has:

fieldtypedescription
order_idintunique order identifier
customer_idintbuyer's identifier
product_idintproduct purchased
quantityintunits purchased (≥ 1)

products — each entry has:

fieldtypedescription
product_idintunique product identifier
namestrproduct name
unit_pricefloatprice per unit
categorystrproduct category

Task

Perform an inner join on product_id (orders that reference a product_id not present in products are silently dropped, and products with no orders are ignored).

For each customer_id that has at least one matched order, compute:

  • total_spent: sum of quantity × unit_price across all matched orders for that customer, rounded to 2 decimal places.
  • order_count: number of matched orders for that customer.
  • top_category: the category (from products) in which the customer spent the most money. In case of a tie, choose the category that comes first alphabetically.

Return a list of dicts, one per customer, each with keys customer_id (int), total_spent (float), order_count (int), and top_category (str).

The list must be sorted by total_spent descending; if two customers have the same total_spent, sort by customer_id ascending.

Constraints

  • 0 ≤ len(orders) ≤ 2 000
  • 0 ≤ len(products) ≤ 2 000
  • product_id values in products are unique.
  • order_id values in orders are unique.
  • 1 ≤ quantity ≤ 1 000
  • 0.0 < unit_price ≤ 10 000.0
  • If orders is empty or no order matches any product, return [].

Examples

Example 1

Input: reconcile_orders([{'order_id': 1, 'customer_id': 101, 'product_id': 10, 'quantity': 2}, {'order_id': 2, 'customer_id': 101, 'product_id': 20, 'quantity': 1}, {'order_id': 3, 'customer_id': 102, 'product_id': 10, 'quantity': 5}], [{'product_id': 10, 'name': 'Widget A', 'unit_price': 9.99, 'category': 'Gadgets'}, {'product_id': 20, 'name': 'Widget B', 'unit_price': 4.5, 'category': 'Gadgets'}, {'product_id': 30, 'name': 'Widget C', 'unit_price': 1, 'category': 'Supplies'}])

Output: [{'customer_id': 102, 'total_spent': 49.95, 'order_count': 1, 'top_category': 'Gadgets'}, {'customer_id': 101, 'total_spent': 24.48, 'order_count': 2, 'top_category': 'Gadgets'}]

Example 2

Input: reconcile_orders([{'order_id': 1, 'customer_id': 1, 'product_id': 5, 'quantity': 3}, {'order_id': 2, 'customer_id': 1, 'product_id': 6, 'quantity': 2}, {'order_id': 3, 'customer_id': 2, 'product_id': 5, 'quantity': 1}, {'order_id': 4, 'customer_id': 2, 'product_id': 99, 'quantity': 10}], [{'product_id': 5, 'name': 'Alpha', 'unit_price': 10, 'category': 'Electronics'}, {'product_id': 6, 'name': 'Beta', 'unit_price': 25, 'category': 'Furniture'}])

Output: [{'customer_id': 1, 'total_spent': 80, 'order_count': 2, 'top_category': 'Furniture'}, {'customer_id': 2, 'total_spent': 10, 'order_count': 1, 'top_category': 'Electronics'}]

Example 3

Input: reconcile_orders([], [{'product_id': 1, 'name': 'Solo', 'unit_price': 5, 'category': 'Misc'}])

Output: []

Hints

Hint 1

Build a dict keyed by product_id from the products list so lookups are O(1) when iterating over orders.

Hint 2

Use a nested dict keyed by customer_id, then category to accumulate spend per category, so finding top_category is a single max() call per customer.

Hint 3

Apply Python's round(value, 2) only at the very end when building the result dicts to avoid compounding rounding errors.