Second Degree Follower - Problem
Second Degree Follower Analysis
In a social media platform, we need to analyze user connections to identify influential users who are both active followers and have their own follower base.
You are given a
A second-degree follower is a user who satisfies both conditions:
1. Follows at least one user (they are active on the platform)
2. Is followed by at least one user (they have influence)
Goal: Find all second-degree followers and count how many people follow them. Return results ordered alphabetically by follower name.
Example: If Alice follows Bob, Bob follows Charlie, and Dave follows Bob, then Bob is a second-degree follower with 1 follower (Dave).
In a social media platform, we need to analyze user connections to identify influential users who are both active followers and have their own follower base.
You are given a
Follow table that tracks follower relationships:| Column Name | Type |
|---|---|
| followee | varchar |
| follower | varchar |
A second-degree follower is a user who satisfies both conditions:
1. Follows at least one user (they are active on the platform)
2. Is followed by at least one user (they have influence)
Goal: Find all second-degree followers and count how many people follow them. Return results ordered alphabetically by follower name.
Example: If Alice follows Bob, Bob follows Charlie, and Dave follows Bob, then Bob is a second-degree follower with 1 follower (Dave).
Input & Output
example_1.sql โ Basic Social Network
$
Input:
Follow table:
+----------+----------+
| followee | follower |
+----------+----------+
| A | B |
| B | C |
| B | D |
| D | E |
+----------+----------+
โบ
Output:
+----------+-----+
| follower | num |
+----------+-----+
| B | 2 |
| D | 1 |
+----------+-----+
๐ก Note:
B follows A and is followed by C and D (2 followers), making B a second-degree follower. D follows B and is followed by E (1 follower), making D a second-degree follower. A only has followers but doesn't follow anyone. C and E only follow but have no followers.
example_2.sql โ Circular Following
$
Input:
Follow table:
+----------+----------+
| followee | follower |
+----------+----------+
| Alice | Bob |
| Bob | Alice |
| Bob | Charlie |
+----------+----------+
โบ
Output:
+----------+-----+
| follower | num |
+----------+-----+
| Alice | 1 |
| Bob | 2 |
+----------+-----+
๐ก Note:
Alice follows Bob and is followed by Bob (1 follower). Bob follows Alice and is followed by Alice and Charlie (2 followers). Charlie only follows Bob but has no followers.
example_3.sql โ No Second Degree Followers
$
Input:
Follow table:
+----------+----------+
| followee | follower |
+----------+----------+
| X | Y |
| Z | W |
+----------+----------+
โบ
Output:
+----------+-----+
| follower | num |
+----------+-----+
+----------+-----+
๐ก Note:
No user satisfies both conditions. Y follows X but has no followers. W follows Z but has no followers. X and Z have followers but don't follow anyone.
Visualization
Tap to expand
Understanding the Visualization
1
Identify Connections
Map out who follows whom in the network
2
Find Bridge Users
Look for users who appear on both sides of relationships
3
Count Influence
Count how many followers each bridge user has
4
Rank by Name
Sort results alphabetically for consistent output
Key Takeaway
๐ฏ Key Insight: Use self-join to efficiently find users who appear in both follower and followee roles, avoiding multiple table scans
Time & Space Complexity
Time Complexity
O(n log n)
Single pass with JOIN operation, potentially using indices
โก Linearithmic
Space Complexity
O(n)
Space for JOIN intermediate results and grouping
โก Linearithmic Space
Constraints
- 1 โค Follow table rows โค 1000
- followee and follower are valid user names (non-empty strings)
- No user follows themselves
- followee โ follower for all rows
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code