KatInterview

Per-Account Usage Means Above a Threshold

SQL & DataAuto-graded · 15 test cases
MediumAuto-gradedSQL & Dataaggregationgroupinghashing

Last updated

Group readings by account id, keep accounts whose unrounded mean CPU, memory, or disk usage tops 50, then emit half-up rounded, email-sorted records.

Two lists of plain dictionaries describe a hosting database.

customers holds account records, each with:

  • "id": an integer account identifier, unique across accounts.
  • "email": a string address. Distinct accounts may carry an identical email string.

readings holds metric records, each with:

  • "customer_id": the account the reading belongs to.
  • "cpu_usage", "memory_usage", "disk_usage": numeric percentages.

For each account that has at least one reading in readings, take the arithmetic mean of its cpu_usage values, the mean of its memory_usage values, and the mean of its disk_usage values. An account qualifies when at least one of these three means is strictly greater than 50. Judge this on the raw means, before any rounding — a mean of exactly 50 does not qualify.

Produce a list of dictionaries, one per qualifying account, each holding exactly these keys:

  • "email"
  • "average_cpu_usage"
  • "average_memory_usage"
  • "average_disk_usage"

The three averages must each be the mean rounded to two decimal places with half-up rounding (a tie at the third decimal moves away from zero). Whether an account qualifies is still decided on the unrounded means, so a mean such as 50.004 is included even though it displays as 50.0.

Ordering rules:

  • Sort the result ascending by email. Accounts that share an email may appear in any relative order among themselves.

Further rules:

  • Accounts with no readings at all are left out.
  • Readings whose "customer_id" matches no account are ignored.
  • Never discard individual readings based on how high or low a value is before averaging.
  • Every qualifying account gets its own output record, even when two accounts share the same email.

Return the result as a plain Python list of dictionaries.

Constraints

  • 0 <= len(customers) <= 10^5
  • 0 <= len(readings) <= 10^5
  • Account "id" values are unique; "customer_id" values in readings are integers.
  • Usage values are non-negative numbers (not None).
  • Each qualifying account contributes exactly one record; the total never exceeds len(customers).

Examples

Example 1

Input: accounts_over_usage_threshold([{'id': 1, 'email': '[email protected]'}, {'id': 2, 'email': '[email protected]'}, {'id': 3, 'email': '[email protected]'}], [{'customer_id': 1, 'cpu_usage': 60, 'memory_usage': 40, 'disk_usage': 45}, {'customer_id': 1, 'cpu_usage': 40, 'memory_usage': 60, 'disk_usage': 45}, {'customer_id': 2, 'cpu_usage': 50, 'memory_usage': 50, 'disk_usage': 50}, {'customer_id': 3, 'cpu_usage': 20, 'memory_usage': 30, 'disk_usage': 70}])

Output: [{'email': '[email protected]', 'average_cpu_usage': 20, 'average_memory_usage': 30, 'average_disk_usage': 70}]

Example 2

Input: accounts_over_usage_threshold([{'id': 1, 'email': '[email protected]'}, {'id': 2, 'email': '[email protected]'}, {'id': 3, 'email': '[email protected]'}], [{'customer_id': 1, 'cpu_usage': 50, 'memory_usage': 50, 'disk_usage': 50}, {'customer_id': 2, 'cpu_usage': 51, 'memory_usage': 49, 'disk_usage': 10}])

Output: [{'email': '[email protected]', 'average_cpu_usage': 51, 'average_memory_usage': 49, 'average_disk_usage': 10}]

Hints

Hint 1

Key the running totals on the account id, not the email, so two accounts that share an address keep their averages apart.

Hint 2

Decide who qualifies using the raw means; round only the three numbers you place in the output.

Hint 3

Add a tiny epsilon before calling round so binary float noise and banker's rounding don't pull x.xx5 values down.