Similar Problems
Similar Problems not available
Find Followers Count - Leetcode Solution
Companies:
LeetCode: Find Followers Count Leetcode Solution
Difficulty: Easy
Topics: database
Problem Statement:
Given a user id, implement a function to find their followers count.
Function Signature:
int find_followers_count(int user_id)
Solution:
One way to solve this problem is to use a graph data structure. We can model the relationship between users and their followers as a directed graph where each node represents a user and each edge represents a follower relationship.
We can use an adjacency list to represent the graph, where each node has a list of its followers. We can then perform a breadth-first search (BFS) starting from the given user id to find all its followers and count them.
Here is the detailed algorithm:
- Create an empty graph object and add the given user id as a node in the graph.
- Query the user id to get its followers and add each follower as a node in the graph and a directed edge from the user id node to the follower node.
- Initialize a count variable to 0.
- Create a set to keep track of visited nodes during BFS.
- Initialize a queue with the user id node.
- While the queue is not empty, do the following:
- Dequeue the next node in the queue.
- If the node has not been visited, set its visited flag and increment the count variable.
- Add all its unvisited follower nodes to the queue.
- Return the count variable.
Here is the pseudocode implementation of the algorithm:
int find_followers_count(int user_id):
// Step 1 - Create graph object and add user id as node
Graph graph = new Graph()
Node user_node = graph.add_node(user_id)
// Step 2 - Query user's followers and add them as nodes in the graph
List<int> followers = query_followers(user_id)
for each follower_id in followers:
Node follower_node = graph.add_node(follower_id)
graph.add_edge(user_node, follower_node)
// Step 3 - Initialize count variable
int count = 0
// Step 4 - Initialize visited set
Set<Node> visited = new Set()
// Step 5 - Initialize BFS queue with user id node
Queue<Node> queue = new Queue()
queue.enqueue(user_node)
// Step 6 - BFS loop
while not queue.empty():
// Dequeue next node from queue
Node node = queue.dequeue()
// Check if node has been visited
if node not in visited:
// Mark node as visited and increment count
visited.add(node)
count += 1
// Add unvisited follower nodes to queue
for each follower_node in graph.get_followers(node):
if follower_node not in visited:
queue.enqueue(follower_node)
// Step 7 - Return count
return count
The time complexity of this algorithm is O(N + F), where N is the number of nodes in the graph (users) and F is the total number of follower relationships. This is because we add each node to the graph only once and we visit each node and its followers at most once during BFS.
The space complexity is also O(N + F) because we store the graph as an adjacency list, the visited nodes in a set, and the BFS queue.
Find Followers Count Solution Code
1