Skip to main content

Second Degree Follower - Solution & Explanation

MediumPremiumFree on FleetCodeDatabase4 min read
Practice this problem

Problem Statement

Table: Follow

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| followee    | varchar |
| follower    | varchar |
+-------------+---------+
(followee, follower) is the primary key (combination of columns with unique values) for this table.
Each row of this table indicates that the user follower follows the user followee on a social network.
There will not be a user following themself.

 

A second-degree follower is a user who:

  • follows at least one user, and
  • is followed by at least one user.

Write a solution to report the second-degree users and the number of their followers.

Return the result table ordered by follower in alphabetical order.

The result format is in the following example.

 

Example 1:

Input: 
Follow table:
+----------+----------+
| followee | follower |
+----------+----------+
| Alice    | Bob      |
| Bob      | Cena     |
| Bob      | Donald   |
| Donald   | Edward   |
+----------+----------+
Output: 
+----------+-----+
| follower | num |
+----------+-----+
| Bob      | 2   |
| Donald   | 1   |
+----------+-----+
Explanation: 
User Bob has 2 followers. Bob is a second-degree follower because he follows Alice, so we include him in the result table.
User Donald has 1 follower. Donald is a second-degree follower because he follows Bob, so we include him in the result table.
User Alice has 1 follower. Alice is not a second-degree follower because she does not follow anyone, so we don not include her in the result table.

Approach Overview

Problem Overview: The Second Degree Follower problem gives a follow table with two columns: follower and followee. A second-degree follower of user C exists when user A follows B and B follows C. The task is to return each followee who has at least one such second-degree follower and count how many distinct users form those relationships.

Approach 1: Self Join + GROUP BY Aggregation (O(n²) time, O(1) extra space)

The clean SQL approach uses a self join on the follow table. Treat the table as two logical copies: f1 and f2. Join them where f1.followee = f2.follower. This join links the first step (A → B) with the second step (B → C), producing the chain that defines a second-degree follower. Once the join produces these pairs, group the results by f2.followee (the final user in the chain) and count distinct f1.follower values.

The key insight is that a second-degree relationship is just a two-hop path in the follow graph. A SQL self join models this path directly. GROUP BY combined with COUNT(DISTINCT ...) ensures each second-degree follower is counted once. Rows are filtered with a HAVING clause so only users with at least one such follower appear in the result.

This pattern appears frequently in relational graph-style problems. When a table represents edges in a social graph, joining the table with itself lets you traverse relationships multiple steps deep. Understanding this technique is essential for many SQL and database interview questions.

Indexes on followee and follower significantly improve performance because the join condition relies on these columns. Without indexes, the database engine may scan the table multiple times, resulting in roughly O(n²) join behavior. With proper indexing, the practical runtime is closer to O(n log n) depending on the optimizer.

Recommended for interviews: The self join with GROUP BY is the expected solution. It demonstrates that you understand relational joins, graph traversal through tables, and aggregation in SQL. Explaining the two-hop relationship and translating it into a self join shows stronger database reasoning than relying on nested subqueries alone. This pattern also appears in problems involving SQL joins and social network graph queries.

Solution

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Self Join + GROUP BYO(n²) without indexesO(1)Standard SQL solution for two-hop relationships in a follow graph
Self Join with Index Optimization~O(n log n)O(1)Preferred in production databases where indexes exist on follower/followee

Video Solution

Leetcode MEDIUM 614 - Second Degree Follower JOIN Solution - Explained by Everyday Data ScienceEveryday Data Science1,053 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Second Degree Follower easy or hard?
Second Degree Follower is rated Medium because it requires recognizing a two-hop relationship and translating it into a SQL self join. Developers familiar with joins and aggregation typically solve it quickly, but it can be tricky if you have not practiced relational graph queries.
Second Degree Follower Python/Java solution
This problem is designed for SQL rather than general programming languages. The expected answer is a MySQL query using a self join and GROUP BY aggregation to compute second-degree follower counts.
How to solve Second Degree Follower in O(n)?
Pure O(n) execution is uncommon for this query because it requires joining relationships between rows. The closest practical optimization uses indexed joins on follower and followee columns, allowing the database engine to quickly match rows and avoid full scans.
What is the best approach for Second Degree Follower?
The standard solution uses a SQL self join. Join the follow table with itself where f1.followee = f2.follower to create a two-hop relationship (A → B → C). Then group by the final followee and count distinct first-hop followers. This approach directly models the second-degree relationship in a relational table.
Is Second Degree Follower asked at Google/Amazon/Meta?
SQL join and aggregation problems like Second Degree Follower commonly appear in database interview rounds at large tech companies. Variations of social graph queries and multi-hop relationships are frequently asked to test SQL join fundamentals.
What data structure is used in Second Degree Follower?
The underlying structure is a relational table representing edges in a directed graph. The SQL solution uses a self join to traverse two edges in the graph and GROUP BY aggregation to count unique second-degree followers.
What is the time complexity of Second Degree Follower?
A self join on the follow table typically runs in O(n²) time without indexing because the database may compare many row pairs. With indexes on follower and followee columns, the query planner can reduce lookup cost and practical performance approaches O(n log n).

Ready to solve this problem?

Practice Second Degree Follower with our built-in code editor and test cases.

Practice on FleetCode