#614
Medium Database Second degree follower
Database
40.8% acceptance
Mar 31, 2026
169
745
No description available.
Solution
Pandas
Time O(1)
Space O(1)
# 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.
import pandas as pd
def second_degree_follower(follow: pd.DataFrame) -> pd.DataFrame:
follows_someone = set(follow['follower'].unique())
is_followed = follow[follow['followee'].isin(follows_someone)]
result = is_followed.groupby('followee')['follower'].nunique().reset_index()
result.columns = ['follower', 'num']
return result.sort_values('follower')