KatInterview

Monthly Revenue Summary by Product Category

SQL & DataAuto-graded · 13 test cases
MediumAuto-gradedSQL & Datagroup-by aggregationdata cleaningdictionaries

Last updated

Tests group-by aggregation over a list of transaction records to compute per-category monthly revenue, count, and average order value.

Monthly Revenue Summary by Product Category

You are given a list of sales transaction records. Each record is a dictionary with the following fields:

FieldTypeDescription
"transaction_id"strUnique identifier for the transaction
"category"strProduct category name
"month"strMonth in "YYYY-MM" format
"amount"floatRevenue amount for the transaction (may be negative for refunds)
"is_cancelled"boolIf True, this transaction should be excluded entirely

Write a function that processes these records and returns a summary list, where each entry represents one (category, month) pair.

Processing Rules

  1. Skip any transaction where is_cancelled is True.
  2. Group remaining transactions by (category, month).
  3. For each group compute:
    • "total_revenue": sum of amount, rounded to 2 decimal places
    • "transaction_count": number of transactions in the group
    • "avg_order_value": total_revenue / transaction_count, rounded to 2 decimal places
  4. Return the summary list sorted first by month ascending, then by category ascending (both lexicographic).
  5. If all transactions are cancelled (or the input list is empty), return an empty list [].

Output Format

Each element of the returned list is a dict with exactly these keys:

{
    "category": str,
    "month": str,
    "total_revenue": float,
    "transaction_count": int,
    "avg_order_value": float
}

Constraints

  • 0 ≤ len(transactions) ≤ 2000
  • Each amount satisfies -10000.0 ≤ amount ≤ 10000.0
  • category is a non-empty string; casing matters ("Books" ≠ "books")
  • month is always a valid "YYYY-MM" string
  • transaction_id values are unique across all records
  • There is no requirement on how many distinct categories or months exist

Examples

Example 1

Input: monthly_revenue_summary([{'transaction_id': 't1', 'category': 'Books', 'month': '2024-01', 'amount': 29.99, 'is_cancelled': False}, {'transaction_id': 't2', 'category': 'Electronics', 'month': '2024-01', 'amount': 199, 'is_cancelled': False}, {'transaction_id': 't3', 'category': 'Books', 'month': '2024-01', 'amount': 14.99, 'is_cancelled': False}, {'transaction_id': 't4', 'category': 'Books', 'month': '2024-02', 'amount': 9.99, 'is_cancelled': False}, {'transaction_id': 't5', 'category': 'Electronics', 'month': '2024-01', 'amount': 49.99, 'is_cancelled': True}])

Output: [{'category': 'Books', 'month': '2024-01', 'total_revenue': 44.98, 'transaction_count': 2, 'avg_order_value': 22.49}, {'category': 'Electronics', 'month': '2024-01', 'total_revenue': 199, 'transaction_count': 1, 'avg_order_value': 199}, {'category': 'Books', 'month': '2024-02', 'total_revenue': 9.99, 'transaction_count': 1, 'avg_order_value': 9.99}]

Example 2

Input: monthly_revenue_summary([{'transaction_id': 'r1', 'category': 'Clothing', 'month': '2023-11', 'amount': -45, 'is_cancelled': False}, {'transaction_id': 'r2', 'category': 'Clothing', 'month': '2023-11', 'amount': 120, 'is_cancelled': False}, {'transaction_id': 'r3', 'category': 'Clothing', 'month': '2023-11', 'amount': 80, 'is_cancelled': True}])

Output: [{'category': 'Clothing', 'month': '2023-11', 'total_revenue': 75, 'transaction_count': 2, 'avg_order_value': 37.5}]

Example 3

Input: monthly_revenue_summary([])

Output: []

Hints

Hint 1

Use a dictionary keyed by (category, month) tuples to accumulate running totals and counts in a single pass over the non-cancelled records.

Hint 2

After building the aggregation dict, compute avg_order_value from the accumulated total and count before rounding — avoid rounding intermediate values.

Hint 3

Sort the final list with a tuple key: (entry['month'], entry['category']) to satisfy the tie-break rule.