Find Followers Count — LeetCode 1729 Python Solution

EasyDatabase
Problem
#1729
Reading time
2 min

Table schema

SQL
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

SQL
+-------------+------+
| 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.

Python solution

Python
import duckdb
import pandas as pd

def solution(followers: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Followers", followers)
    return con.execute("""SELECT user_id, COUNT(1) AS followers_count
FROM Followers
GROUP BY 1
ORDER BY 1;""").df()

Complexity

MeasureComplexity
TimeO(n log n) (typical)
SpaceO(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 1729. Find Followers Count?
LeetCode 1729. Find Followers Count is rated Easy on LeetCode.
What topics does LeetCode 1729. Find Followers Count cover?
LeetCode 1729. Find Followers Count is tagged Database on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview