{x}
blog image

First and Last Call On the Same Day

Solution Explanation

This problem requires identifying users whose first and last calls on any given day were with the same person. The solution uses SQL window functions to efficiently achieve this.

Approach:

  1. Union Calls: We first create a common table expression (CTE) called s. This CTE combines the caller and recipient roles into a single column (caller_id), simplifying subsequent processing. Each row represents a call, regardless of whether the user was the caller or recipient.

  2. Window Functions: Another CTE t is created. It leverages the FIRST_VALUE() window function twice for each user on each day.

    • FIRST_VALUE(recipient_id) OVER (PARTITION BY DATE_FORMAT(call_time, '%Y-%m-%d'), caller_id ORDER BY call_time ASC): This finds the recipient ID of the first call of the day for each user. The PARTITION BY clause groups calls by day and user, while ORDER BY call_time ASC ensures the first call is selected.
    • FIRST_VALUE(recipient_id) OVER (PARTITION BY DATE_FORMAT(call_time, '%Y-%m-%d'), caller_id ORDER BY call_time DESC): This similarly finds the recipient ID of the last call of the day for each user, using ORDER BY call_time DESC.
  3. Filtering Results: Finally, we select distinct user_id values from t where the first and last recipient IDs are the same. This condition ensures that the first and last calls on any day were with the same person.

MySQL Code:

with s as (
    select
        *
    from
        Calls
    union
    all
    select
        recipient_id,
        caller_id,
        call_time
    from
        Calls
),
t as (
    select
        caller_id user_id,
        FIRST_VALUE(recipient_id) over(
            partition by DATE_FORMAT(call_time, '%Y-%m-%d'),
            caller_id
            order by
                call_time asc
        ) first,
        FIRST_VALUE(recipient_id) over(
            partition by DATE_FORMAT(call_time, '%Y-%m-%d'),
            caller_id
            order by
                call_time desc
        ) last
    from
        s
)
select
    distinct user_id
from
    t
where
    first = last;

Time Complexity Analysis:

The time complexity of this SQL query is dominated by the window functions. Window functions generally have a time complexity of O(N log N) or O(N) depending on the specific database implementation and query optimization. In this case, N represents the total number of rows in the Calls table. The UNION ALL operation also has a linear time complexity, O(N). Therefore, the overall time complexity is approximately O(N log N) or O(N). The exact time complexity may vary slightly depending on the database system's optimization strategies.

Space Complexity Analysis:

The space complexity is determined primarily by the size of the CTEs (s and t). The size of s is proportional to the number of rows in the Calls table (O(N)). The size of t is also proportional to the number of rows in s, again O(N). Therefore, the overall space complexity is O(N).