Table: Relations
+-------------+------+ | Column Name | Type | +-------------+------+ | user_id | int | | follower_id | int | +-------------+------+ (user_id, follower_id) is the primary key (combination of columns with unique values) for this table. Each row of this table indicates that the user with ID follower_id is following the user with ID user_id.
Write a solution to find all the pairs of users with the maximum number of common followers. In other words, if the maximum number of common followers between any two users is maxCommon, then you have to return all pairs of users that have maxCommon common followers.
The result table should contain the pairs user1_id and user2_id where user1_id < user2_id.
Return the result table in any order.
The result format is in the following example.
Example 1:
Input: Relations table: +---------+-------------+ | user_id | follower_id | +---------+-------------+ | 1 | 3 | | 2 | 3 | | 7 | 3 | | 1 | 4 | | 2 | 4 | | 7 | 4 | | 1 | 5 | | 2 | 6 | | 7 | 5 | +---------+-------------+ Output: +----------+----------+ | user1_id | user2_id | +----------+----------+ | 1 | 7 | +----------+----------+ Explanation: Users 1 and 2 have two common followers (3 and 4). Users 1 and 7 have three common followers (3, 4, and 5). Users 2 and 7 have two common followers (3 and 4). Since the maximum number of common followers between any two users is 3, we return all pairs of users with three common followers, which is only the pair (1, 7). We return the pair as (1, 7), not as (7, 1). Note that we do not have any information about the users that follow users 3, 4, and 5, so we consider them to have 0 followers.
Problem Overview: The Relations table stores which follower_id follows which user_id. The task is to find every pair of users who share the largest number of common followers. If multiple pairs have the same maximum count, return all of them.
Approach 1: Self Join + GROUP BY Aggregation (O(n²) time, O(n) space)
The core idea is to compare users who share the same follower. Perform a self join on the Relations table using follower_id so rows with the same follower are paired together. Enforce r1.user_id < r2.user_id to avoid duplicate and reversed pairs. Each joined row represents one common follower for a pair of users. Use GROUP BY on the pair and count rows to compute the number of shared followers. After computing counts for every pair, filter only those whose count equals the global maximum.
This pattern is common in SQL interview problems where relationships must be compared within the same table. The heavy lifting happens in the join and aggregation step.
Approach 2: Self Join + Window Function Ranking (O(n²) time, O(n) space)
Another clean implementation uses the same self join but replaces the separate max calculation with a window function. After grouping pairs and counting shared followers, apply MAX(cnt) OVER() or a ranking window such as DENSE_RANK(). This computes the global maximum in the same result set. Then filter rows where the count equals that maximum. The logic stays compact and avoids an extra subquery.
This approach relies on features commonly used in SQL joins and analytical queries. It is easier to extend if you later need the top k pairs instead of only the maximum.
Approach 3: Self Join + Subquery Max Filter (O(n²) time, O(n) space)
First compute the number of common followers for each pair using the self join and GROUP BY. Store this intermediate result in a derived table. A nested subquery calculates MAX(common_count) from that result. Finally, return only rows where the pair's count equals that maximum value. This is the most common MySQL implementation because it is straightforward and works without window functions.
The approach highlights two fundamental SQL techniques: relational comparison through self joins and aggregation using GROUP BY.
Recommended for interviews: The self join with GROUP BY and a max filter is the expected solution. It demonstrates that you understand relational joins, deduplication with user_id < user_id, and aggregation logic. A brute-force mindset—comparing every user pair—shows the intuition, but expressing it efficiently in SQL using joins and grouping is what interviewers want to see.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Self Join + GROUP BY | O(n²) | O(n) | Standard SQL solution; works in all relational databases |
| Self Join + Subquery Max Filter | O(n²) | O(n) | Clean MySQL implementation when window functions are unnecessary |
| Self Join + Window Function | O(n²) | O(n) | Modern SQL engines supporting analytic functions |
INSTAGRAM LeetCode Medium “Pairs With Maximum Common Followers" 1951 Interview SQL Question | EDS • Everyday Data Science • 2,772 views views
Practice All the Pairs With the Maximum Number of Common Followers with our built-in code editor and test cases.
Practice on FleetCode