Regional Notification Performance Summary
SQL & DataAuto-graded · 13 test casesLast updated
Tests join, filter, and conditional aggregation logic to compute per-region metrics with threshold filtering and safe division.
You have two data sources representing user accounts and their notification events.
Account data includes:
account_id(integer)region(string)is_active(boolean)
Notification data includes:
account_id(integer)status(string): one of'sent','delivered', or'opened'
Your task is to compute summary statistics by region, considering only accounts where is_active is True.
For each region, calculate:
total_sent: the count of all notifications linked to active accounts in that regionopen_rate: the ratio of notifications with status'opened'to notifications with status'delivered'
Return results only for regions that have at least 100 total notifications. If a region has zero delivered notifications, set its open_rate to 0.
Return a list of dictionaries, one per qualifying region, sorted by region name in ascending order. Each dictionary must contain keys: "region", "total_sent", and "open_rate".
Constraints
1 ≤number of accounts≤ 10^51 ≤number of notifications≤ 10^6- Region names are non-empty strings
- All
account_idvalues in accounts are unique - Notification
account_idvalues may reference accounts not present in the accounts list (treat these as inactive) - Each notification belongs to exactly one account
- Output must be sorted by region name lexicographically
Examples
Example 1
Input: region_notification_metrics([[1, 'NA', True], [2, 'NA', True], [3, 'EU', True]], [[1, 'sent'], [1, 'delivered'], [1, 'opened']])
Output: []
Hints
Hint 1
Build a lookup from account_id to region for only the active accounts, then scan notifications once to update per-region counters.
Hint 2
Track three separate counts per region: total notifications, those marked delivered, and those marked opened.
Hint 3
Apply the 100-notification threshold after aggregation is complete, not during the counting phase.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires joining account and notification data, filtering by account status, then computing conditional aggregates grouped by region.
The strategy is to make a single pass through the notifications after building an index of active accounts:
-
Index active accounts: Create a hash map from
account_idtoregionfor all accounts whereis_activeisTrue. This allows O(1) lookups during notification processing. -
Aggregate by region: Iterate through all notifications. For each notification, look up its account in the active accounts map. If found, increment three counters for that region:
- Total count (all notifications)
- Delivered count (status is
'delivered') - Opened count (status is
'opened')
-
Filter and compute rate: After processing all notifications, examine each region. Keep only those with at least 100 total notifications. For each qualifying region, compute the open rate as opened / delivered, returning 0 when delivered is 0 to avoid division by zero.
-
Sort output: Return results sorted by region name in ascending lexicographic order.
Why It Works
Each notification is counted exactly once in the region of its owning account, and only if that account is active. Notifications linked to inactive or non-existent accounts are excluded, matching inner join semantics. The three counters are maintained consistently over the same filtered set of notifications, ensuring the rate calculation is meaningful. The threshold is applied after aggregation, so it correctly compares against the full regional count.
Complexity
Time: O(A + N + R log R), where A is the number of accounts, N is the number of notifications, and R is the number of distinct regions. Building the active account map takes O(A), processing notifications takes O(N), and sorting output by region takes O(R log R).
Space: O(A + R) for the account map and per-region counter storage.
Solution
from collections import defaultdict
from typing import Dict, List
def region_notification_metrics(accounts: List[List[object]], notifications: List[List[object]]) -> List[Dict[str, object]]:
# Build map of active account_id -> region
active_regions = {
row[0]: row[1]
for row in accounts
if row[2] is True
}
# Track [total, delivered, opened] per region
counts = defaultdict(lambda: [0, 0, 0])
for account_id, status in notifications:
if account_id not in active_regions:
continue
region = active_regions[account_id]
counts[region][0] += 1
if status == "delivered":
counts[region][1] += 1
elif status == "opened":
counts[region][2] += 1
result = []
for region in sorted(counts):
total, delivered, opened = counts[region]
if total >= 100:
result.append({
"region": region,
"total_sent": total,
"open_rate": opened / delivered if delivered else 0,
})
return result