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 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 minutes

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 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
Phone Call Relationship MappingPerson1Person2Call 1: 5minCall 2: 3minNormalization ProcessLEAST(1,2) = 1, GREATEST(1,2) = 2Both calls โ†’ (1,2) groupFinal ResultPair (1,2):2 calls, 8 min totalSQL Query BreakdownSELECTLEAST(from_id, to_id)as person1,GREATEST(from_id, to_id)as person2,COUNT(*)as call_count,SUM(duration)as total_durationFROM CallsGROUP BY person1, person2
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

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Where k is the number of unique person pairs

n
2n
โœ“ 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
Asked in
Meta 45 Amazon 38 Google 32 Microsoft 28
34.8K Views
Medium Frequency
~15 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