Number of Calls Between Two Persons - Problem
Phone Call Analytics Challenge
You work for a telecommunications company and need to analyze call data to understand communication patterns between users. Given a database table
The
โข
โข
โข
Key insight: Since phone calls are bidirectional relationships, a call from person A to person B should be grouped with calls from person B to person A. You need to report the total number of calls and total call duration for each unique pair, ensuring that
๐ Goal: Transform the raw call data into meaningful communication statistics between each pair of distinct persons.
You work for a telecommunications company and need to analyze call data to understand communication patterns between users. Given a database table
Calls that records phone call information, your task is to group and aggregate call statistics for each unique pair of people.The
Calls table contains:โข
from_id: ID of the person making the callโข
to_id: ID of the person receiving the callโข
duration: Length of the call in minutesKey insight: Since phone calls are bidirectional relationships, a call from person A to person B should be grouped with calls from person B to person A. You need to report the total number of calls and total call duration for each unique pair, ensuring that
person1 < person2 to avoid duplicate pairs.๐ Goal: Transform the raw call data into meaningful communication statistics between each pair of distinct persons.
Input & Output
example_1.sql โ Basic Call Pattern
$
Input:
Calls table:
+------+------+----------+
| from_id | to_id | duration |
+------+------+----------+
| 1 | 2 | 59 |
| 2 | 1 | 11 |
| 1 | 3 | 20 |
| 3 | 4 | 100 |
| 3 | 4 | 200 |
| 3 | 4 | 200 |
| 4 | 3 | 499 |
+------+------+----------+
โบ
Output:
+----------+----------+------------+----------------+
| person1 | person2 | call_count | total_duration |
+----------+----------+------------+----------------+
| 1 | 2 | 2 | 70 |
| 1 | 3 | 1 | 20 |
| 3 | 4 | 4 | 999 |
+----------+----------+------------+----------------+
๐ก Note:
Person 1 and 2 had 2 calls (1โ2: 59min, 2โ1: 11min) totaling 70 minutes. Person 1 and 3 had 1 call totaling 20 minutes. Person 3 and 4 had 4 calls (3โ4: 100+200+200=500min, 4โ3: 499min) totaling 999 minutes.
example_2.sql โ Multiple Bidirectional Calls
$
Input:
Calls table:
+------+------+----------+
| from_id | to_id | duration |
+------+------+----------+
| 1 | 2 | 10 |
| 2 | 1 | 15 |
| 2 | 1 | 20 |
| 1 | 2 | 25 |
+------+------+----------+
โบ
Output:
+----------+----------+------------+----------------+
| person1 | person2 | call_count | total_duration |
+----------+----------+------------+----------------+
| 1 | 2 | 4 | 70 |
+----------+----------+------------+----------------+
๐ก Note:
All 4 calls are between persons 1 and 2 (regardless of direction). Total calls: 4, Total duration: 10+15+20+25 = 70 minutes.
example_3.sql โ Single Direction Only
$
Input:
Calls table:
+------+------+----------+
| from_id | to_id | duration |
+------+------+----------+
| 1 | 2 | 30 |
| 1 | 3 | 40 |
| 2 | 3 | 50 |
+------+------+----------+
โบ
Output:
+----------+----------+------------+----------------+
| person1 | person2 | call_count | total_duration |
+----------+----------+------------+----------------+
| 1 | 2 | 1 | 30 |
| 1 | 3 | 1 | 40 |
| 2 | 3 | 1 | 50 |
+----------+----------+------------+----------------+
๐ก Note:
Each pair has only one call in one direction. The pairs are automatically normalized to ensure person1 < person2 format.
Visualization
Tap to expand
Understanding the Visualization
1
Raw Call Records
Individual call records with from_id โ to_id direction
2
Normalize Pairs
Use LEAST/GREATEST to ensure smaller ID comes first
3
Group & Aggregate
Count calls and sum duration for each normalized pair
Key Takeaway
๐ฏ Key Insight: The LEAST/GREATEST functions automatically normalize bidirectional relationships, ensuring (A,B) and (B,A) become the same group (min(A,B), max(A,B))
Time & Space Complexity
Time Complexity
O(n)
Single pass through the data with grouping operation
โ Linear Growth
Space Complexity
O(k)
Where k is the number of unique person pairs
โ Linear Space
Constraints
- 1 โค from_id, to_id โค 105
- 1 โค duration โค 104
- 1 โค Number of calls โค 105
- from_id โ to_id (no self-calls)
- The table may contain duplicate call records
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code