KatInterview

Summarise Monthly Revenue by Region from Transaction Records

SQL & DataAuto-graded · 15 test cases
MediumAuto-gradedSQL & Datadata manipulationaggregationgroup-by

Last updated

Tests group-by aggregation over a list-of-dict dataset, requiring candidates to compute per-region monthly totals, fill gaps, and apply a tie-break for ranking.

Monthly Revenue Summary by Region

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

FieldTypeDescription
"transaction_id"strUnique identifier for the transaction
"region"strRegion name (non-empty string)
"month"strMonth in "YYYY-MM" format
"amount"floatRevenue amount (may be negative for refunds)

Write a function that, given this list of transaction records and a list of query (region, month) pairs, returns for each query the total revenue for that (region, month) combination, rounded to 2 decimal places.

Additionally, the function must return the top-k regions by total revenue across all records (summed over all months), as a list of region names sorted in descending order of total revenue. If two regions have the same total revenue, break the tie alphabetically (ascending) by region name.

Return value

Return a single dict with exactly two keys:

  • "query_results": a list of float values, one per query in the same order as the input queries list. If a (region, month) pair has no matching records, return 0.0 for that query.
  • "top_k_regions": a list of exactly k region names (strings). If fewer than k distinct regions exist in the data, return all distinct regions sorted by the same rule.

Constraints

  • 0 ≤ len(transactions) ≤ 2000
  • Each transaction["amount"] is a finite float in the range [-1,000,000, 1,000,000].
  • 1 ≤ len(queries) ≤ 500
  • Each query is a two-element list [region, month] where month matches the format "YYYY-MM".
  • 1 ≤ k ≤ 50
  • Region names and month strings may contain uppercase and lowercase letters, digits, hyphens, and spaces.
  • Multiple transactions may share the same (region, month) pair.
  • Amounts should be summed with standard floating-point arithmetic before rounding.

Examples

Example 1

Input: monthly_revenue_summary([{'transaction_id': 't1', 'region': 'North', 'month': '2024-01', 'amount': 100}, {'transaction_id': 't2', 'region': 'North', 'month': '2024-01', 'amount': 50}, {'transaction_id': 't3', 'region': 'South', 'month': '2024-01', 'amount': 200}, {'transaction_id': 't4', 'region': 'North', 'month': '2024-02', 'amount': 75}], [['North', '2024-01'], ['South', '2024-01'], ['East', '2024-01']], 2)

Output: {'query_results': [150, 200, 0], 'top_k_regions': ['North', 'South']}

Example 2

Input: monthly_revenue_summary([{'transaction_id': 'r1', 'region': 'Alpha', 'month': '2023-06', 'amount': 500}, {'transaction_id': 'r2', 'region': 'Beta', 'month': '2023-06', 'amount': 500}, {'transaction_id': 'r3', 'region': 'Alpha', 'month': '2023-07', 'amount': -100}], [['Alpha', '2023-06'], ['Beta', '2023-06'], ['Alpha', '2023-07'], ['Gamma', '2023-06']], 2)

Output: {'query_results': [500, 500, -100, 0], 'top_k_regions': ['Beta', 'Alpha']}

Example 3

Input: monthly_revenue_summary([], [['North', '2024-01']], 3)

Output: {'query_results': [0], 'top_k_regions': []}

Hints

Hint 1

Use a nested dict (or collections.defaultdict) keyed by (region, month) to accumulate totals, then look up each query in O(1).

Hint 2

For the top-k ranking, separately accumulate each region's grand total across all months, then sort with a key that captures (negative total, region name) so ties resolve alphabetically.

Hint 3

Remember to round each query result to 2 decimal places using Python's built-in round(), and guard against missing keys by defaulting to 0.0.