KatInterview

Reconcile Sales Orders with Product Catalog

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

Last updated

Tests joining two lists of dicts (orders + products) on a key, then aggregating revenue and applying data-cleaning rules.

Reconcile Sales Orders with Product Catalog

You are given two tables represented as lists of dictionaries:

orders — each dict has:

FieldTypeDescription
order_idstrUnique order identifier
product_idstrReferences a product
quantityintUnits ordered (≥ 1)
discount_pctfloatDiscount percentage in [0.0, 100.0)

products — each dict has:

FieldTypeDescription
product_idstrUnique product identifier
categorystrProduct category name
unit_pricefloatPrice per unit (> 0)

Task

Perform an inner join on product_id, then compute the net revenue for each order:

net_revenue = quantity × unit_price × (1 - discount_pct / 100)

Return a list of dicts, one per category, with the following fields:

FieldTypeDescription
categorystrCategory name
total_revenuefloatSum of net revenue for all matched orders in this category, rounded to 2 decimal places
order_countintNumber of matched orders in this category
avg_discount_pctfloatMean discount_pct across matched orders in this category, rounded to 2 decimal places

Data-cleaning rules

  1. Unmatched orders (no product with that product_id) are silently dropped — they do not appear in any category.
  2. If unit_price ≤ 0 or quantity ≤ 0 for a matched row, treat that row's net_revenue as 0.0 (but still count the order and include its discount_pct in the average).
  3. If the products table contains duplicate product_ids, use the last occurrence (by list position).

Output ordering

Sort the result list by total_revenue descending. Break ties alphabetically by category ascending.

Constraints

  • 0 ≤ len(orders) ≤ 2000
  • 0 ≤ len(products) ≤ 2000
  • product_id and category are non-empty strings.
  • discount_pct ∈ [0.0, 100.0)
  • unit_price > 0 in well-formed data (but see cleaning rule 2).
  • Returns [] when no orders survive the inner join.

Examples

Example 1

Input: reconcile_orders([{'order_id': 'O1', 'product_id': 'P1', 'quantity': 2, 'discount_pct': 10}, {'order_id': 'O2', 'product_id': 'P2', 'quantity': 1, 'discount_pct': 0}, {'order_id': 'O3', 'product_id': 'P3', 'quantity': 5, 'discount_pct': 20}], [{'product_id': 'P1', 'category': 'Electronics', 'unit_price': 100}, {'product_id': 'P2', 'category': 'Books', 'unit_price': 15}, {'product_id': 'P3', 'category': 'Electronics', 'unit_price': 40}])

Output: [{'category': 'Electronics', 'total_revenue': 340, 'order_count': 2, 'avg_discount_pct': 15}, {'category': 'Books', 'total_revenue': 15, 'order_count': 1, 'avg_discount_pct': 0}]

Example 2

Input: reconcile_orders([{'order_id': 'O1', 'product_id': 'GHOST', 'quantity': 3, 'discount_pct': 5}], [{'product_id': 'P1', 'category': 'Toys', 'unit_price': 9.99}])

Output: []

Example 3

Input: reconcile_orders([{'order_id': 'O1', 'product_id': 'P1', 'quantity': 4, 'discount_pct': 25}, {'order_id': 'O2', 'product_id': 'P1', 'quantity': 2, 'discount_pct': 50}], [{'product_id': 'P1', 'category': 'Sports', 'unit_price': 50}, {'product_id': 'P1', 'category': 'Outdoors', 'unit_price': 80}])

Output: [{'category': 'Outdoors', 'total_revenue': 320, 'order_count': 2, 'avg_discount_pct': 37.5}]

Hints

Hint 1

Build a dict from the products list keyed by product_id (last occurrence wins) before iterating over orders — this gives O(1) lookups.

Hint 2

Use a second dict keyed by category to accumulate total_revenue, order_count, and the sum of discount_pct, then convert to a list at the end.

Hint 3

Apply the data-cleaning check (quantity ≤ 0 or unit_price ≤ 0) before adding to total_revenue, but always increment order_count and add to the discount sum.