KatInterview

Longest Average Calls Leaderboard Since a Cutoff

SQL & DataAuto-graded · 14 test cases
MediumAuto-gradedSQL & Datagroup-byaggregationdatetime

Last updated

Filter call records by start time, group by user to get average duration in minutes, then rank with a deterministic tie-break and keep the top 10.

A video-calling team keeps a log of calls. Each record is a dictionary with string values:

  • call_id: identifier of the call
  • user_id: identifier of the user who made the call
  • start_time: when the call began, formatted YYYY-MM-DD HH:MM:SS
  • end_time: when the call ended, in the same format

The team wants a leaderboard of the users whose calls were longest on average within a recent time window. The start of the window is passed in explicitly as window_start, so the result does not depend on the current clock.

Write a function that returns the 10 users with the highest average call duration, measured in minutes.

Rules

  1. Window filter. A record is counted only if its start_time is greater than or equal to window_start. The boundary is inclusive. Only start_time is compared, so a call that began before the window is ignored even if it ended inside it. Ignored records are skipped entirely.
  2. Every record counts. Each counted record is its own call. Do not deduplicate by call_id, and do not drop zero-length calls.
  3. Duration. A counted call's duration in minutes is (end_time - start_time) expressed in seconds, divided by 60, as a floating-point number. Do not round or truncate. For example, 45 minutes 30 seconds is 45.5. A duration is used as computed, even if it is zero.
  4. Average. For each user, add up the durations of that user's counted calls, starting from 0.0 and adding in the order the records appear in the input. Then divide by the number of counted calls. Users with no counted calls do not appear. Use exactly this arithmetic (per-call seconds / 60, summed, then divided by the count), because ties are decided by comparing these resulting floating-point averages for equality.
  5. Ordering. Sort users by average duration, largest first. If two users have equal averages, put the one with the smaller user_id first. Compare user_id values exactly as given, as plain strings (character-by-character, so "10" comes before "9").
  6. Limit. Return at most 10 users. If fewer qualify, return all of them. If none qualify, return an empty list.

Output

Return a list of dictionaries, one per ranked user, in rank order. Each dictionary has exactly two keys:

  • user_id: the user's identifier, unchanged from the input
  • average_duration_minutes: the user's average call duration in minutes, as a number

Constraints

  • Every record has all four fields.
  • All timestamps and window_start are valid and use the format YYYY-MM-DD HH:MM:SS.
  • The number of records can be large, so a single pass over the data is expected.
  • The result must follow the filter, arithmetic, ordering and tie-break rules above exactly.

Examples

Example 1

Input: longest_average_calls([{'call_id': '1', 'user_id': '101', 'start_time': '2023-07-01 09:00:00', 'end_time': '2023-07-01 09:45:00'}, {'call_id': '2', 'user_id': '102', 'start_time': '2023-07-01 10:15:00', 'end_time': '2023-07-01 10:40:00'}, {'call_id': '3', 'user_id': '101', 'start_time': '2023-07-02 14:05:00', 'end_time': '2023-07-02 14:25:00'}, {'call_id': '4', 'user_id': '103', 'start_time': '2023-07-02 15:00:00', 'end_time': '2023-07-02 16:30:00'}, {'call_id': '5', 'user_id': '102', 'start_time': '2023-07-03 11:20:00', 'end_time': '2023-07-03 12:00:00'}], '2023-06-27 00:00:00')

Output: [{'user_id': '103', 'average_duration_minutes': 90}, {'user_id': '101', 'average_duration_minutes': 32.5}, {'user_id': '102', 'average_duration_minutes': 32.5}]

Hints

Hint 1

Handle each call independently: parse the start time, skip the record if it is before the window start, and only then look at the end time.

Hint 2

Keep two dictionaries keyed by user: a running total of minutes (add each call's total_seconds() / 60 in input order) and a count of calls. The average is total divided by count.

Hint 3

Once you have per-user averages, sort with a key of (-average, user_id) so larger averages come first and ties fall back to ascending string user_id, then slice the first 10.