Count Followers for Each Observed User

Quick Overview

Return user_id and followers_count for each user represented as user_id in Followers. The unique (user_id,follower_id) pairs prevent duplicate follow relationships. Sort user_id ascending.

Count Followers for Each Observed User

Company: Google

Role: Software Engineer

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

### Problem Return user_id and followers_count for each user represented as user_id in Followers. The unique (user_id,follower_id) pairs prevent duplicate follow relationships. Sort user_id ascending. ### Tables Table: `Followers` ```text +-------------+------+ | 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. ``` ### Example ```text Followers (user_id,follower_id): (5,1), (5,2), (2,5), (9,5) Output (user_id,followers_count): (2,1), (5,2), (9,1) ``` ### What a Strong Answer Covers - Count the recorded followers of each followed user. - Return only users present as a followed user and sort by user ID. ### Follow-up Questions - How would you include users who have no followers? - How would you handle repeated follow events if the relationship key were removed?

Overview: Return user_id and followers_count for each user represented as user_id in Followers. The unique (user_id,follower_id) pairs prevent duplicate follow relationships. Sort user_id ascending.

Google logo
Google
easySoftware EngineerOtherData Manipulation (SQL/Python)
0
0

Problem

Return user_id and followers_count for each user represented as user_id in Followers. The unique (user_id,follower_id) pairs prevent duplicate follow relationships. Sort user_id ascending.

Tables

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.

Example

Followers (user_id,follower_id): (5,1), (5,2), (2,5), (9,5)
Output (user_id,followers_count): (2,1), (5,2), (9,1)

What a Strong Answer Covers Guidance

  • Count the recorded followers of each followed user.
  • Return only users present as a followed user and sort by user ID.

Follow-up Questions Guidance

  • How would you include users who have no followers?
  • How would you handle repeated follow events if the relationship key were removed?