KatInterview

Calculate Call Answer Metrics for Active Users

SQL & DataAuto-graded · 14 test cases
EasyAuto-gradedSQL & Datasqlarrayshash-table

Last updated

Process call records and lifecycle logs to compute the success rate of answered calls for eligible users within a specific time frame.

Given log data from a communication platform, compute the call answer rate for every user who initiated at least one call within the target 30-day period (2023-10-01 through 2023-10-31, inclusive).

You are given two structures:

  1. calls: A list of call creation records where each record is represented as [call_id, user_id, timestamp].

    • call_id (int): Unique identifier for the call.
    • user_id (int): User who initiated the call.
    • timestamp (str): ISO formatted date/time string (e.g., "2023-10-01" or "2023-10-01 14:30:00").
  2. events: A list of status update events where each event is represented as [call_id, event_type, event_time].

    • call_id (int): Associated call ID.
    • event_type (str): Status update type ("pickup" or "missed").
    • event_time (str): ISO formatted date/time string of the event.

Requirements:

  • Filter for calls initiated between 2023-10-01 and 2023-10-31 inclusive (determined by extracting the date part YYYY-MM-DD from the call timestamp).
  • Calculate the pickup_rate for each eligible user: $$\text{pickup_rate} = \frac{\text{Number of unique in-window calls that were answered}}{\text{Total number of in-window calls initiated by the user}}$$
  • Due to duplicate event logging, a call is considered answered if it has at least one "pickup" event. Ignore duplicate event records for the same call.
  • Users who did not initiate any calls in the target date range must not appear in the final output.
  • Return a list of pairs [[user_id, pickup_rate], ...] ordered by user_id in ascending order.

Constraints:

  • 1 <= len(calls) <= 10^4
  • 0 <= len(events) <= 2 * 10^4
  • Timestamps are ISO formatted strings. The date component can be extracted from the first 10 characters (timestamp[:10]).

Examples

Example 1

Input: call_pickup_rates([[1, 101, '2023-10-01'], [2, 101, '2023-10-05']], [[1, 'pickup', '2023-10-01'], [2, 'missed', '2023-10-05']])

Output: [[101, 0.5]]

Hints

Hint 1

Filter calls first based on the date range '2023-10-01' <= date <= '2023-10-31' to determine eligible calls and valid users.

Hint 2

Deduplicate pickup events per call so that multiple logged pickup events for a single call do not inflate the call count.

Hint 3

Ensure users with eligible calls but zero picked up calls receive a rate of 0.0, and sort the final output by user_id.