KatInterview

Consolidate E-Commerce Orders into Fact Records

SQL & DataAuto-graded · 14 test cases
MediumAuto-gradedSQL & Datasqlarraysdata-manipulation

Last updated

Process and aggregate relational records across multiple entity tables into a single denormalized fact dataset.

You are tasked with building a data pipeline function that denormalizes multiple entity datasets into a unified fact table centered around order line items.

Write a function build_facts that receives seven lists of dictionaries representing tables: customers, orders, order_items, products, payments, shipments, and refunds. The function must produce a list of dictionaries where each record corresponds to a single line item in order_items.

Each returned dictionary must contain exactly the following keys, ordered as specified:

  1. order_item_id
  2. order_id
  3. customer_id
  4. order_ts
  5. paid_ts
  6. shipped_ts
  7. product_id
  8. sku
  9. category
  10. qty
  11. unit_price
  12. line_amount
  13. refund_amount
  14. country
  15. status
  16. payment_method
  17. carrier

Field Calculation and Aggregation Rules:

  • Base Grain: The output contains one dictionary per element in order_items, preserving the input sequence of order_items.
  • Line Amount: Computed as qty * unit_price.
  • Refund Amount: Sum of refund_amount across all entries in refunds belonging to the specific order_item_id. If there are no refunds for an item, default to 0 (or 0.0).
  • Customer & Product Details: Joined via order_id -> orders -> customer_id -> customers and product_id -> products. If a lookup reference is missing, populate missing fields with None.
  • Payment Attributes:
    • paid_ts: The maximum (latest) paid_ts among all payments associated with the item's order_id. If no payments exist, set to None.
    • payment_method: The method of the payment with the highest amount for the item's order_id. If there is a tie for the maximum amount, select the lexicographically smaller method. If no payments exist, set to None.
  • Shipment Attributes:
    • shipped_ts: The minimum (earliest) shipped_ts among all shipments for the item's order_id. If no shipments exist, set to None.
    • carrier: The minimum (lexicographically smallest) non-None carrier across shipments for the item's order_id. If no valid carrier exists, set to None.

Constraints:

  • order_items length $0 \le N \le 10,000$.
  • ISO 8601 formatted string timestamps or None values can be compared directly or using standard datetime parsing.
  • String fields may be None if entity relationships are missing or unassigned.

Examples

Example 1

Input: build_facts([], [], [{'order_item_id': 1, 'order_id': 10, 'product_id': 100, 'qty': 2, 'unit_price': 3.5}], [{'product_id': 100, 'sku': 'A-1', 'category': 'books'}], [], [], [])

Output: [{'order_item_id': 1, 'order_id': 10, 'customer_id': None, 'order_ts': None, 'paid_ts': None, 'shipped_ts': None, 'product_id': 100, 'sku': 'A-1', 'category': 'books', 'qty': 2, 'unit_price': 3.5, 'line_amount': 7, 'refund_amount': 0, 'country': None, 'status': None, 'payment_method': None, 'carrier': None}]

Example 2

Input: build_facts([{'customer_id': 7, 'signup_date': '2024-01-01T00:00:00', 'country': 'US'}], [{'order_id': 10, 'customer_id': 7, 'order_ts': '2024-02-01T12:00:00', 'status': 'complete'}], [{'order_item_id': 1, 'order_id': 10, 'product_id': 100, 'qty': 2, 'unit_price': 3.5}], [{'product_id': 100, 'sku': 'A-1', 'category': 'books'}], [{'payment_id': 1, 'order_id': 10, 'paid_ts': '2024-02-02T09:00:00', 'method': 'card', 'amount': 7}, {'payment_id': 2, 'order_id': 10, 'paid_ts': '2024-02-03T09:00:00', 'method': 'wallet', 'amount': 7}], [{'shipment_id': 1, 'order_id': 10, 'shipped_ts': '2024-02-04T09:00:00', 'carrier': 'Parcel', 'ship_cost': 2}], [{'refund_id': 1, 'order_item_id': 1, 'refund_ts': '2024-02-05T09:00:00', 'refund_amount': 1.25, 'reason': 'return'}])

Output: [{'order_item_id': 1, 'order_id': 10, 'customer_id': 7, 'order_ts': '2024-02-01T12:00:00', 'paid_ts': '2024-02-03T09:00:00', 'shipped_ts': '2024-02-04T09:00:00', 'product_id': 100, 'sku': 'A-1', 'category': 'books', 'qty': 2, 'unit_price': 3.5, 'line_amount': 7, 'refund_amount': 1.25, 'country': 'US', 'status': 'complete', 'payment_method': 'card', 'carrier': 'Parcel'}]

Hints

Hint 1

Index static entity datasets (customers, orders, products) using hash maps keying on their primary IDs for O(1) lookups.

Hint 2

Group payments and shipments by order_id prior to iterating over items so that aggregations do not re-scan the arrays.

Hint 3

For payment_method ties, use a custom sort key tuple (-amount, method) to easily determine the winning record.