Calculate Call Answer Metrics for Active Users
SQL & DataAuto-graded · 14 test casesLast 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:
-
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").
-
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-01and2023-10-31inclusive (determined by extracting the date partYYYY-MM-DDfrom the calltimestamp). - Calculate the
pickup_ratefor 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 byuser_idin ascending order.
Constraints:
1 <= len(calls) <= 10^40 <= 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.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem requires us to process call metadata and status updates to derive the pickup rate per active user over a fixed time period (2023-10-01 to 2023-10-31 inclusive).
-
Filtering Target Calls:
- Iterate through the
callslist and extract the date part (timestamp[:10]). - If the date falls within October 2023, map
call_idtouser_idand increment the call count for that user (denominator).
- Iterate through the
-
Deduplicating Events:
- Multiple status update logs may exist for the same call due to retry or system logs.
- We sort
eventsbyevent_timeand record unique(call_id, event_type)pairs. - If an event has
event_type == 'pickup'and itscall_idis among the target calls, we mark thatcall_idas answered.
-
Computing Pickup Rates:
- Count the total number of unique answered calls per user (numerator).
- Compute
pickup_rate = numerator / denominatorfor each user who initiated calls in the date range. - Sort the output pairs by
user_idin ascending order.
Complexity Analysis
- Time Complexity: $\mathcal{O}(C + E \log E)$ where $C$ is the number of call entries and $E$ is the number of lifecycle event entries. Sorting events by timestamp takes $\mathcal{O}(E \log E)$, while dictionary lookup operations take $\mathcal{O}(1)$ average time.
- Space Complexity: $\mathcal{O}(C + E)$ to store mappings for valid calls, deduplicated status logs, and pickup counts.
from collections import defaultdict
from datetime import date
from typing import List
def call_pickup_rates(calls: List[List], events: List[List]) -> List[List]:
start = date(2023, 10, 1)
end = date(2023, 10, 31)
call_users = {}
totals = defaultdict(int)
for call_id, user_id, timestamp in calls:
day = date.fromisoformat(timestamp[:10])
if start <= day <= end:
call_users[call_id] = user_id
totals[user_id] += 1
picked_up = set()
first_status = set()
for call_id, event_type, event_time in sorted(events, key=lambda event: event[2]):
key = (call_id, event_type)
if key in first_status:
continue
first_status.add(key)
if event_type == 'pickup' and call_id in call_users:
picked_up.add(call_id)
pickup_counts = defaultdict(int)
for call_id in picked_up:
pickup_counts[call_users[call_id]] += 1
return [[user_id, pickup_counts[user_id] / total]
for user_id, total in sorted(totals.items())]