Watch 10 video solutions for Find Followers Count, a easy level problem involving Database. This walkthrough by Learn With Chirag has 4,342 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Table: Followers
+-------------+------+ | Column Name | Type | +-------------+------+ | user_id | int | | follower_id | int | +-------------+------+ (user_id, follower_id) is the primary key (combination of columns with unique values) for this table. This table contains the IDs of a user and a follower in a social media app where the follower follows the user.
Write a solution that will, for each user, return the number of followers.
Return the result table ordered by user_id in ascending order.
The result format is in the following example.
Example 1:
Input:
Followers table:
+---------+-------------+
| user_id | follower_id |
+---------+-------------+
| 0 | 1 |
| 1 | 0 |
| 2 | 0 |
| 2 | 1 |
+---------+-------------+
Output:
+---------+----------------+
| user_id | followers_count|
+---------+----------------+
| 0 | 1 |
| 1 | 1 |
| 2 | 2 |
+---------+----------------+
Explanation:
The followers of 0 are {1}
The followers of 1 are {0}
The followers of 2 are {0,1}
Problem Overview: You are given a Followers table where each row represents a follower relationship: (user_id, follower_id). The task is to compute how many followers each user has and return the results sorted by user_id.
Approach 1: SQL-like Aggregation (O(n) time, O(k) space)
The most direct way to solve this database-style problem is grouping records by user_id and counting how many rows belong to each group. In SQL this is done with GROUP BY and COUNT(*). When implemented in languages like Python or JavaScript, you simulate this behavior by grouping rows by key and incrementing a counter. The key insight is that every row contributes exactly one follower to the corresponding user_id. This approach scans the dataset once and maintains a running count per user.
Conceptually this mirrors database aggregation operations covered in database problems. The grouping step ensures each user appears once in the result set with their follower total.
Approach 2: Hash Map / Dictionary Counting (O(n) time, O(k) space)
Another way to think about the same operation is explicit frequency counting using a hash map. Iterate through every row in the table and store counts in a dictionary where the key is user_id and the value is the follower count. Each iteration performs a constant-time hash lookup and increments the stored value. After processing all rows, sort the keys and output the results.
This approach relies on a hash table to maintain counts efficiently. It mirrors how many database engines internally implement aggregation operations. Because each relationship is processed once, the runtime remains linear in the number of rows.
Recommended for interviews: The aggregation idea is the expected solution since the problem is fundamentally a grouping query. Explaining the SQL GROUP BY version shows you understand relational operations. Implementing the same logic with a hash map demonstrates algorithmic thinking and gives an optimal O(n) time solution with O(k) space, where k is the number of unique users.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| SQL Aggregation (GROUP BY) | O(n) | O(k) | Best for database queries or SQL-style problems where grouping and counting rows is required |
| Hash Map / Dictionary Counting | O(n) | O(k) | Useful when implementing the logic in general-purpose languages like C++, C#, Python, or JavaScript |