Given a social network Follow table with followee and follower columns, find all second-degree followers.
A second-degree follower is a user who:
- Follows at least one user (appears as follower)
- Is followed by at least one user (appears as followee)
Return the second-degree users and their follower count, ordered by follower alphabetically.
Table Schema
| Column Name | Type | Description |
|---|---|---|
followee
PK
|
varchar | User being followed |
follower
PK
|
varchar | User who follows |
Input & Output
| followee | follower |
|---|---|
| A | B |
| A | C |
| B | D |
| follower | num |
|---|---|
| B | 1 |
User B follows A (appears as follower) and is followed by D (appears as followee). B is a second-degree follower with 1 follower (D). Users A and C only appear as followee/follower respectively, so they don't qualify.
| followee | follower |
|---|---|
| A | B |
| B | C |
| B | D |
| D | E |
| follower | num |
|---|---|
| B | 2 |
| D | 1 |
User B follows A and is followed by C and D (2 followers). User D follows B and is followed by E (1 follower). Both are second-degree followers, ordered alphabetically.
| followee | follower |
|---|---|
| A | B |
| C | D |
| follower | num |
|---|
No user appears in both roles. B only follows, A and C are only followed, D only follows. No second-degree followers exist.
Constraints
-
followee != follower(no self-following) -
1 ≤ number of relationships ≤ 1000 - All usernames are valid varchar strings