First and Last Call On the Same Day - Problem
Phone Call Pattern Analysis
You're given a
The table structure:
Key Rules:
Example: If User 1 calls User 2 at 9 AM and User 2 calls User 1 at 11 PM on the same day, and these are User 1's first and last calls that day, then User 1 should be in the result.
You're given a
Calls table that tracks phone conversations between users. Your task is to find users whose first call and last call on any given day were with the same person.The table structure:
| Column Name | Type |
|---|---|
| caller_id | int |
| recipient_id | int |
| call_time | datetime |
Key Rules:
- Calls count regardless of whether the user is caller or recipient
- We're looking for patterns within the same day
- A user must have made at least 2 calls in a day to qualify
- Return user IDs who show this pattern on any day
Example: If User 1 calls User 2 at 9 AM and User 2 calls User 1 at 11 PM on the same day, and these are User 1's first and last calls that day, then User 1 should be in the result.
Input & Output
example_1.sql โ Basic Pattern Match
$
Input:
Calls table:\n| caller_id | recipient_id | call_time |\n|-----------|--------------|---------------------|\n| 1 | 2 | 2020-07-29 09:00:00 |\n| 2 | 1 | 2020-07-29 17:00:00 |\n| 1 | 3 | 2020-07-29 12:00:00 |
โบ
Output:
| user_id |\n|---------|\n| 1 |
๐ก Note:
User 1's calls on 2020-07-29: First call with User 2 (09:00), call with User 3 (12:00), last call with User 2 (17:00 as recipient). Since first and last calls were with User 2, User 1 is included in result.
example_2.sql โ Multiple Users Pattern
$
Input:
Calls table:\n| caller_id | recipient_id | call_time |\n|-----------|--------------|---------------------|\n| 1 | 2 | 2020-07-29 09:00:00 |\n| 1 | 3 | 2020-07-29 17:00:00 |\n| 2 | 3 | 2020-07-29 11:00:00 |\n| 3 | 2 | 2020-07-29 18:00:00 |
โบ
Output:
| user_id |\n|---------|\n| 2 |
๐ก Note:
User 2's calls on 2020-07-29: First call with User 1 (09:00 as recipient), last call with User 3 (18:00 as recipient). Different partners, so User 2 is NOT included. Only users with matching first/last call partners qualify.
example_3.sql โ Single Call Edge Case
$
Input:
Calls table:\n| caller_id | recipient_id | call_time |\n|-----------|--------------|---------------------|\n| 1 | 2 | 2020-07-29 09:00:00 |\n| 3 | 4 | 2020-07-29 10:00:00 |
โบ
Output:
| user_id |\n|---------|
๐ก Note:
Each user made only one call that day. Since we need at least 2 calls to have distinct 'first' and 'last' calls, no users qualify for the result.
Constraints
- 1 โค calls.length โค 105
- 1 โค caller_id, recipient_id โค 103
- caller_id != recipient_id
- call_time is a valid datetime in format 'YYYY-MM-DD HH:MM:SS'
Visualization
Tap to expand
Understanding the Visualization
1
Normalize Call Data
Transform caller/recipient data into user perspective - every call appears twice, once for each participant
2
Rank Daily Calls
For each user-date, rank calls by time: 1st, 2nd, 3rd... and also rank in reverse: last, 2nd-to-last...
3
Extract First & Last
Identify who was called first (rank 1 ascending) and last (rank 1 descending) each day
4
Pattern Matching
Find users where first_call_partner = last_call_partner for any day with 2+ calls
Key Takeaway
๐ฏ Key Insight: Window functions provide an elegant solution by ranking calls within user-date partitions, allowing us to efficiently identify first and last call patterns in a single database scan.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code