KatInterview

Find Top-Funded Municipalities and Their Leading Projects

SQL & DataAuto-graded · 18 test cases
MediumAuto-gradedSQL & Dataarrayshash-tablesorting

Last updated

Aggregate relational records over a specific date window to identify peak entities with tie preservation at multiple hierarchical levels.

You are analyzing development grants allocated across various municipalities. You are given three datasets representing municipalities, local projects, and financial grant records.

Your goal is to identify all municipalities that achieved the highest aggregate funding received throughout the full calendar year of 2024 (specifically covering timestamps from 2024-01-01 00:00:00 up to, but excluding, 2025-01-01 00:00:00).

For each municipality that achieves this maximum total 2024 funding, you must also determine the project or projects within that municipality that received the highest individual 2024 funding total.

Input Formats

  1. villages: A list of dictionaries, where each dictionary contains:
    • "village_id" (int): Unique identifier for the municipality.
    • "village_name" (str): Name of the municipality.
  2. projects: A list of dictionaries, where each dictionary contains:
    • "project_id" (int): Unique identifier for the project.
    • "village_id" (int): Id of the associated municipality.
    • "project_name" (str): Name of the project.
  3. project_fundings: A list of dictionaries, where each dictionary contains:
    • "funding_id" (int): Unique identifier for the grant transaction.
    • "project_id" (int): Id of the recipient project.
    • "amount" (int or float): Disbursed funds.
    • "funded_at" (str): Timestamp formatted as "YYYY-MM-DD HH:MM:SS".

Rules & Requirements

  1. Date Filtering: Only sum disbursements where 2024-01-01 00:00:00 <= funded_at < 2025-01-01 00:00:00.
  2. Aggregation:
    • A project's 2024 total funding is the sum of all valid 2024 disbursements it received (0 if none).
    • A municipality's 2024 total funding is the sum of the 2024 funding totals of all projects registered under it.
  3. Preserve Ties at Both Levels:
    • If multiple municipalities share the global maximum total 2024 funding, include all of them.
    • For any selected municipality, if multiple projects share the maximum 2024 funding within that municipality, include all of them.
  4. Empty Municipalities/Projects: A municipality with no registered projects does not produce any output rows.
  5. Output Structure: Return a list of lists, where each inner list contains: [village_id, village_name, village_total_funding, project_id, project_name, project_total_funding] Convert funding totals to standard floating-point numbers (float).
  6. Ordering: Sort the output rows in ascending order by village_id, and then by project_id.

Constraints

  • 0 <= len(villages) <= 1000
  • 0 <= len(projects) <= 5000
  • 0 <= len(project_fundings) <= 20000
  • amount >= 0
  • All IDs are positive integers.

Examples

Example 1

Input: top_funded_village_projects([{'village_id': 1, 'village_name': 'Greenfield'}, {'village_id': 2, 'village_name': 'Riverdale'}, {'village_id': 3, 'village_name': 'Oakridge'}], [{'project_id': 101, 'village_id': 1, 'project_name': 'Solar Well'}, {'project_id': 102, 'village_id': 1, 'project_name': 'Road Repair'}, {'project_id': 103, 'village_id': 2, 'project_name': 'Clinic Expansion'}, {'project_id': 104, 'village_id': 2, 'project_name': 'School Library'}, {'project_id': 105, 'village_id': 3, 'project_name': 'Irrigation Canal'}], [{'funding_id': 1, 'project_id': 101, 'amount': 3000, 'funded_at': '2024-03-15 10:00:00'}, {'funding_id': 2, 'project_id': 101, 'amount': 2000, 'funded_at': '2024-06-01 09:00:00'}, {'funding_id': 3, 'project_id': 102, 'amount': 5000, 'funded_at': '2024-08-10 12:00:00'}, {'funding_id': 4, 'project_id': 103, 'amount': 6000, 'funded_at': '2024-04-20 11:00:00'}, {'funding_id': 5, 'project_id': 104, 'amount': 4000, 'funded_at': '2024-09-05 14:00:00'}, {'funding_id': 6, 'project_id': 105, 'amount': 9000, 'funded_at': '2023-12-31 23:59:59'}])

Output: [[1, 'Greenfield', 10000, 101, 'Solar Well', 5000], [1, 'Greenfield', 10000, 102, 'Road Repair', 5000], [2, 'Riverdale', 10000, 103, 'Clinic Expansion', 6000]]

Hints

Hint 1

Filter and sum funding allocations per project for the year 2024 first before combining with municipality data.

Hint 2

Using `Decimal` for precision during aggregation avoids floating-point inaccuracy when evaluating ties.

Hint 3

Identify the maximum municipality total across all municipalities, filter for tied municipalities, and then find the top project(s) per tied municipality.