Write SQL for influence score and follower growth
RobloxData Scientist · Technical ScreenLast updated
This question evaluates SQL data manipulation competencies such as joins, aggregations, distinct counts, window functions, and time-based grouping for computing an influence score (engagements on a user's posts plus follower count) and monthly follower growth, and it falls under Data Manipulation (SQL/Python).
You are working on a social product with these tables:
Tables / Schemas
users
user_idBIGINT (PK)created_atTIMESTAMP
posts
post_idBIGINT (PK)author_idBIGINT (FK → users.user_id)created_atTIMESTAMP
engagement (one row per engagement event)
engagement_idBIGINT (PK)post_idBIGINT (FK → posts.post_id)viewer_idBIGINT (FK → users.user_id)event_typeSTRING (e.g.,like,comment,share)created_atTIMESTAMP
follows (directed edge)
follower_idBIGINT (FK → users.user_id)followee_idBIGINT (FK → users.user_id)created_atTIMESTAMP (when the follow happened)
Assume timestamps are in UTC, and “month” means DATE_TRUNC('month', created_at).
1) Influence score
Define a user’s influence score as:
[ \text{influence_score}(u) = #\text{engagement events received on u's posts} + #\text{followers of u} ]
Write a SQL query returning:
user_idinfluence_score
Notes:
- Engagement “received” means engagement rows on posts where
posts.author_id = user_id. - Followers count is the number of distinct
follower_idwherefollowee_id = user_id.
2) Monthly follower growth
For each user (as the followee), compute monthly follower growth metrics.
Write a SQL query that outputs, for each followee_id and month:
monthfollowee_idnew_followers(distinct new followers gained that month)prev_month_new_followers(previous month’snew_followersusingLAG)mom_growth(month-over-month growth rate:(new_followers - prev_month_new_followers) / prev_month_new_followers, handling divide-by-zero)cumulative_followers(running total across months using a windowSUM)
Assume unfollows are not tracked (so follower counts only increase).
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.