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 Follow table that tracks follower relationships:

Column NameType
followeevarchar
followervarchar

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
AliceBobCharlieDavidEvefollowsfollowsfollowsfollowsSecond-Degree Followers AnalysisBob:Follows Alice, followed by Charlie & David โ†’ 2 followersDavid:Follows Bob, followed by Eve โ†’ 1 followerAlice, Charlie, Eve: Only follow OR are followed (not both)
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

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for JOIN intermediate results and grouping

n
2n
โšก 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
Asked in
Meta 45 LinkedIn 38 Twitter 32 Instagram 25
28.4K Views
Medium Frequency
~15 min Avg. Time
892 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