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 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
Social Network Common Followers AnalysisUser1User2F1F2F3F4F5Users to CompareTheir FollowersCommon Followers: F1, F2Count = 2SQL Logic1. JOIN Relations r1 with Relations r2 ON r1.follower_id = r2.follower_id2. WHERE r1.user_id < r2.user_id (avoid duplicates)3. Use MAX() OVER() window function to find global maximum efficiently
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

n
2n
โš  Quadratic Growth
Space Complexity
O(nยฒ)

Space needed to store all user pairs and their common follower counts

n
2n
โš  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
Asked in
Meta 45 Google 35 Amazon 28 Microsoft 22
36.9K Views
Medium Frequency
~25 min Avg. Time
1.6K 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