KatInterview

Aggregate Transactions by Country for Most Recent Active Period

SQL & DataAuto-graded · 22 test cases
MediumAuto-gradedSQL & Dataaggregationdate-manipulationfiltering

Last updated

Tests date handling, filtering, and grouped aggregation to find the latest month with successful records and summarize by region.

Problem Statement

You are building a financial reporting tool that processes transaction records across multiple organizations. Your task is to generate a summary showing how transactions are distributed across different regions for the most recent period that had activity.

Write a function that identifies the latest calendar month (determined by transaction date) containing at least one successful transaction (status 'completed'), then returns aggregated metrics for each organization's headquarters country during that specific month.

Input Format

The function receives two lists:

companies

Each element is a list: [company_id, company_name, hq_country]

  • company_id (int): Unique identifier for the organization
  • company_name (str): Organization name
  • hq_country (str): Headquarters country code or name

bookings

Each element is a list: [booking_id, company_id, booking_date, booking_amount, status]

  • booking_id (int): Unique identifier for the transaction
  • company_id (int): Reference to the organization
  • booking_date (str): Transaction date in ISO format (YYYY-MM-DD)
  • booking_amount (float): Transaction amount
  • status (str): One of 'completed', 'failed', or 'pending'

Output Format

Return a list of lists, where each inner list contains:

  • hq_country (str): The headquarters country
  • total_amount (float): Sum of all completed transaction amounts for that country in the target month, rounded to 2 decimal places
  • booking_count (int): Count of completed transactions for that country in the target month

Sort results by total_amount descending, then by hq_country ascending (alphabetically).

Rules and Constraints

  • Only transactions with status 'completed' count toward determining the active month
  • A month is "active" only if it contains at least one completed transaction
  • Only include transactions that belong to organizations present in the companies list
  • If no completed transactions exist, return an empty list
  • Transactions in months containing only failed or pending transactions should be ignored
  • Round the total amount to exactly 2 decimal places
  • If multiple countries have the same total amount, order them alphabetically by country code

Constraints

  • 0 ≤ len(companies) ≤ 10,000
  • 0 ≤ len(bookings) ≤ 100,000
  • All dates are valid ISO format strings
  • Amounts are non-negative
  • Status values are always one of: 'completed', 'failed', 'pending'

Examples

Example 1

Input: aggregate_latest_month_by_region([[1, 'Acme Corp', 'US'], [2, 'Beta Ltd', 'UK'], [3, 'Gamma GmbH', 'DE']], [[101, 1, '2024-02-15', 10000, 'completed'], [102, 2, '2024-02-28', 15000.5, 'completed'], [103, 1, '2024-03-05', 12000, 'completed'], [104, 2, '2024-03-12', 8000, 'failed'], [105, 3, '2024-03-20', 5000.25, 'completed'], [106, 1, '2024-04-02', 4000, 'failed']])

Output: [['US', 12000, 1], ['DE', 5000.25, 1]]

Hints

Hint 1

First identify which calendar month is the latest one containing any completed transaction, then filter all data to only that month

Hint 2

Build a lookup dictionary mapping company IDs to their countries before processing transactions

Hint 3

Track both the sum and count for each country separately, then combine them at the end