KatInterview

Regional Notification Performance Summary

SQL & DataAuto-graded · 13 test cases
MediumAuto-gradedSQL & Dataaggregationjoinsconditional-logic

Last 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:

  1. total_sent: the count of all notifications linked to active accounts in that region
  2. open_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^5
  • 1 ≤ number of notifications ≤ 10^6
  • Region names are non-empty strings
  • All account_id values in accounts are unique
  • Notification account_id values 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.