First and Last Call On the Same Day - Problem
Phone Call Pattern Analysis

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 NameType
caller_idint
recipient_idint
call_timedatetime

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
๐Ÿ“ž Daily CallsUser 1 โ†’ User 2 (9 AM)User 1 โ†’ User 3 (12 PM)User 2 โ†’ User 1 (5 PM)๐Ÿ”„ NormalizedUser 1 โ†” User 2 (9 AM) Rank: 1stUser 1 โ†” User 3 (12 PM) Rank: 2ndUser 1 โ†” User 2 (5 PM) Rank: 3rdโœจ Pattern FoundUser 1:First call: User 2Last call: User 2Window Functions Magic ๐Ÿช„ROW_NUMBER() OVER (PARTITION BY user_id, date ORDER BY call_time ASC) โ†’ First call rankROW_NUMBER() OVER (PARTITION BY user_id, date ORDER BY call_time DESC) โ†’ Last call rank๐ŸŽฏ Key InsightWindow functions let us rank calls by time within each user-date group,making it easy to identify first and last call partners efficiently!Single pass through data with O(n log n) complexity
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.
Asked in
Meta 45 Amazon 38 Google 32 Microsoft 28
42.0K Views
High Frequency
~25 min Avg. Time
1.4K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen