Magicsheet logo

All the Pairs With the Maximum Number of Common Followers

Medium
100%
Updated 8/1/2025

Asked by 1 Company

Topics

All the Pairs With the Maximum Number of Common Followers

What is this problem about?

The "All the Pairs With the Maximum Number of Common Followers interview question" is a database query task involving a social media data model. You are given a table of followers (who follows whom). Your goal is to find pairs of users who share the highest number of common followers. You need to return all such pairs that tie for the maximum count.

Why is this asked in interviews?

This "All the Pairs With the Maximum Number of Common Followers coding problem" is common in data-heavy interviews because it requires joining a table on itself to find intersections. It tests a candidate's ability to use aggregate functions (COUNT), grouping (GROUP BY), and filtering based on an aggregate value (using HAVING or window functions like RANK).

Algorithmic pattern used

This problem follows the Intersection via Self-Join pattern.

  1. Find Commonality: Join the Followers table with itself on the follower_id. This identifies pairs of users who have the same follower.
  2. Count: Group by the two users in the pair and count the occurrences. This gives you the number of common followers for every pair.
  3. Maximize: Use a subquery or a Common Table Expression (CTE) to find the maximum count, and then filter the grouped results to only include those that match this maximum.

Example explanation

Data:

  • User A followed by: {1, 2, 3}
  • User B followed by: {2, 3, 4}
  • User C followed by: {1, 5} Processing:
  • Pair (A, B) share: {2, 3} -> Count = 2.
  • Pair (A, C) share: {1} -> Count = 1.
  • Pair (B, C) share: {} -> Count = 0. Max common followers is 2. The result is the pair (A, B).

Common mistakes candidates make

  • Double Counting: Returning both (A, B) and (B, A). Usually, you should filter using User1 < User2 to get unique pairs.
  • Inefficiency: Forgetting to group correctly, leading to massive intermediate join tables.
  • Not handling ties: Only returning one pair when multiple pairs might have the same maximum number of common followers.

Interview preparation tip

Practice the pattern of "Finding common items between two entities." This is a standard SQL technique. Also, get comfortable using the RANK() window function, as it is often the cleanest way to find the "top" items in a dataset including ties.

Similar Questions