All the Pairs With the Maximum Number of Common Followers - Problem
Find User Pairs with Maximum Common Followers
You're analyzing a social media platform where users follow each other. Given a
The
Each row indicates that
1. Calculate how many common followers each pair of users has
2. Find the maximum number of common followers among all pairs
3. Return all user pairs that achieve this maximum
Output format: Return pairs as
You're analyzing a social media platform where users follow each other. Given a
Relations table that tracks follower relationships, your task is to find all pairs of users who share the maximum number of common followers.The
Relations table has the following structure:+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id | int |
| follower_id | int |
+-------------+------+Each row indicates that
follower_id is following user_id. You need to:1. Calculate how many common followers each pair of users has
2. Find the maximum number of common followers among all pairs
3. Return all user pairs that achieve this maximum
Output format: Return pairs as
(user1_id, user2_id) where user1_id < user2_id Input & Output
example_1.sql โ Basic Case
$
Input:
Relations table:
| user_id | follower_id |
|---------|-------------|
| 1 | 3 |
| 1 | 4 |
| 2 | 3 |
| 2 | 4 |
| 2 | 5 |
| 6 | 1 |
โบ
Output:
| user1_id | user2_id |
|----------|----------|
| 1 | 2 |
๐ก Note:
Users 1 and 2 have 2 common followers (3 and 4). This is the maximum number of common followers among all possible pairs, so (1,2) is returned.
example_2.sql โ Multiple Pairs with Same Max
$
Input:
Relations table:
| user_id | follower_id |
|---------|-------------|
| 1 | 5 |
| 1 | 6 |
| 2 | 5 |
| 2 | 6 |
| 3 | 5 |
| 3 | 6 |
| 4 | 7 |
โบ
Output:
| user1_id | user2_id |
|----------|----------|
| 1 | 2 |
| 1 | 3 |
| 2 | 3 |
๐ก Note:
Users 1, 2, and 3 all share 2 common followers (5 and 6). All three pairs (1,2), (1,3), and (2,3) have the maximum count of 2, so all are returned.
example_3.sql โ Single Follower Maximum
$
Input:
Relations table:
| user_id | follower_id |
|---------|-------------|
| 1 | 3 |
| 2 | 3 |
| 4 | 5 |
| 6 | 7 |
โบ
Output:
| user1_id | user2_id |
|----------|----------|
| 1 | 2 |
๐ก Note:
Only users 1 and 2 have any common followers (follower 3). Since 1 is the maximum number of common followers, pair (1,2) is returned.
Visualization
Tap to expand
Understanding the Visualization
1
Identify Relationships
Map out all follower-user relationships in the social network
2
Find Intersections
For each pair of users, find followers they have in common using SQL JOIN
3
Count & Compare
Count common followers and use window functions to find the global maximum
4
Select Winners
Return all user pairs that achieve the maximum overlap
Key Takeaway
๐ฏ Key Insight: Using SQL window functions allows us to find the global maximum in a single pass, making the solution both elegant and efficient for large social networks.
Time & Space Complexity
Time Complexity
O(nยฒ log n)
O(nยฒ) to generate pairs and count common followers, O(log n) for sorting operations
โ Quadratic Growth
Space Complexity
O(nยฒ)
Space needed to store all user pairs and their common follower counts
โ Quadratic Space
Constraints
- 1 โค Relations.length โค 104
- 1 โค user_id, follower_id โค 109
- user_id โ follower_id
- Each (user_id, follower_id) pair is unique
- At least one pair of users must exist
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code