Find Followers Count
You are given a dataset representing users and their follow relationships on a social media platform. Your task is to determine, for each user, how many other users are following them. This is a common operation for social media analytics and for displaying follower counts on user profiles.
Problem Description
You will be provided with a list of "follow" actions. Each action signifies that one user is following another. Your goal is to calculate the total number of followers for each unique user present in the dataset.
Key Requirements:
- You need to process a collection of follow relationships.
- For every user, you must output their total follower count.
- A user can follow multiple other users, and multiple users can follow the same user.
- If a user is not followed by anyone, their follower count should be 0.
- The output should clearly associate each user with their respective follower count.
Expected Behavior:
The output should be a structure (like a map or dictionary) where keys are user identifiers and values are the integer counts of their followers.
Edge Cases:
- A user might be in the dataset but not follow anyone, and also not be followed by anyone. Their count should be 0.
- A user might follow others but not be followed themselves. Their count should be 0.
- The input list of follow actions could be empty.
Examples
Example 1:
Input:
Follow actions: [
{"follower": "Alice", "followed": "Bob"},
{"follower": "Charlie", "followed": "Bob"},
{"follower": "Alice", "followed": "David"}
]
Output:
Follower counts: {
"Bob": 2,
"David": 1,
"Alice": 0,
"Charlie": 0
}
Explanation: Bob is followed by Alice and Charlie (2 followers). David is followed by Alice (1 follower). Alice is not followed by anyone in this list (0 followers). Charlie is not followed by anyone in this list (0 followers).
Example 2:
Input:
Follow actions: [
{"follower": "UserA", "followed": "UserB"},
{"follower": "UserB", "followed": "UserA"},
{"follower": "UserC", "followed": "UserA"}
]
Output:
Follower counts: {
"UserA": 2,
"UserB": 1,
"UserC": 0
}
Explanation: UserA is followed by UserB and UserC (2 followers). UserB is followed by UserA (1 follower). UserC is not followed by anyone in this list (0 followers).
Example 3: (Empty input)
Input:
Follow actions: []
Output:
Follower counts: {}
Explanation: With no follow actions, there are no users to report counts for, resulting in an empty output.
Constraints
- The number of follow actions will be between 0 and 100,000.
- User identifiers will be strings, and their length will be between 1 and 30 characters.
- The solution should aim to be efficient, ideally completing within a reasonable time for the given constraints (e.g., O(N) or O(N log N) where N is the number of follow actions).
Notes
- Consider how to efficiently store and update the follower counts for each user.
- You will need to account for all users mentioned in the follow actions, both as followers and as followed individuals.
- A map or dictionary data structure would be suitable for storing the results.