KatInterview

Creator Reach Score and Monthly New-Follower Trends

SQL & DataAuto-graded · 16 test cases
EasyAuto-gradedSQL & Dataaggregationjoinswindow-functions

Last updated

Join and aggregate social-graph records to compute per-user reach scores and per-month follower statistics, including previous-month lookups, growth rates and running totals.

You are given four in-memory "tables" for a social app, each a list of dictionaries. Write a function that computes two reports from them.

Input

  • accounts: one dict per user, with key user_id.
  • articles: one dict per post, with keys post_id and author_id.
  • interactions: one dict per engagement event, with keys post_id, viewer_id and event_type (for example like, comment, share).
  • connections: one dict per follow relationship, with keys follower_id, followee_id and created_at. created_at is a UTC timestamp, normally an ISO-8601 string such as "2024-01-05T12:00:00Z". A datetime object may also appear.

The month of a follow is its created_at formatted as "YYYY-MM".

Report 1: reach score

For every user in accounts, the reach score is:

reach = (number of interaction events on posts written by the user) + (number of distinct followers of the user)

  • Every event counts once, whatever its event_type.
  • An event whose post_id does not appear in articles is ignored.
  • Followers are counted by distinct follower_id among the rows where followee_id is the user, so repeated follow rows from the same person count once.
  • Self-follows and self-engagement are counted like any other.
  • Users with no posts or followers get a score of 0.

Return one entry per user in accounts, as {"user_id": ..., "influence_score": ...}, sorted by user_id ascending.

Report 2: monthly follower trends

Group connections by (followee_id, month). For each group, produce a row with:

  • month: the "YYYY-MM" string.
  • followee_id.
  • new_followers: the number of distinct follower_id values in that group.
  • prev_month_new_followers: the new_followers of that followee's previous row in time order, or None for the followee's first row. "Previous" means the previous month that has at least one follow row. Calendar gaps are not filled with zeros.
  • mom_growth: (new_followers - prev_month_new_followers) / prev_month_new_followers as a float, or None when there is no previous row or the previous value is 0.
  • cumulative_followers: the sum of new_followers over this row and all earlier rows of the same followee. This is a plain running sum of the monthly counts, so a person who follows in two different months is counted in both.

Report 2 is built from connections alone. It does not depend on accounts. Sort the rows by followee_id ascending, then by month ascending.

Output

Return a dict with two keys:

{"influence": [...report 1 rows...], "monthly_growth": [...report 2 rows...]}

Constraints

  • All four lists may be empty. Then both reports are empty lists.
  • Assume user_id values in accounts are unique.
  • Follows are never undone, so there are no unfollow events.
  • Input sizes are modest, so one pass over each list with hash maps is enough.

Examples

Example 1

Input: compute_creator_metrics([{'user_id': 1}, {'user_id': 2}, {'user_id': 3}], [{'post_id': 10, 'author_id': 1}, {'post_id': 11, 'author_id': 2}], [{'post_id': 10, 'viewer_id': 2, 'event_type': 'like'}, {'post_id': 10, 'viewer_id': 3, 'event_type': 'comment'}, {'post_id': 11, 'viewer_id': 1, 'event_type': 'like'}], [{'follower_id': 2, 'followee_id': 1, 'created_at': '2024-01-05T12:00:00Z'}, {'follower_id': 3, 'followee_id': 1, 'created_at': '2024-02-02T09:00:00Z'}, {'follower_id': 2, 'followee_id': 1, 'created_at': '2024-02-10T10:00:00Z'}])

Output: {'influence': [{'user_id': 1, 'influence_score': 4}, {'user_id': 2, 'influence_score': 1}, {'user_id': 3, 'influence_score': 0}], 'monthly_growth': [{'month': '2024-01', 'followee_id': 1, 'new_followers': 1, 'prev_month_new_followers': None, 'mom_growth': None, 'cumulative_followers': 1}, {'month': '2024-02', 'followee_id': 1, 'new_followers': 2, 'prev_month_new_followers': 1, 'mom_growth': 1, 'cumulative_followers': 3}]}

Example 2

Input: compute_creator_metrics([], [], [], [])

Output: {'influence': [], 'monthly_growth': []}

Example 3

Input: compute_creator_metrics([{'user_id': 5}], [{'post_id': 7, 'author_id': 5}], [{'post_id': 7, 'viewer_id': 5, 'event_type': 'like'}], [{'follower_id': 5, 'followee_id': 5, 'created_at': '2023-12-31T23:59:59Z'}])

Output: {'influence': [{'user_id': 5, 'influence_score': 2}], 'monthly_growth': [{'month': '2023-12', 'followee_id': 5, 'new_followers': 1, 'prev_month_new_followers': None, 'mom_growth': None, 'cumulative_followers': 1}]}

Hints

Hint 1

Build a lookup from post_id to author_id first. Then each interaction can be credited to an author in O(1), and interactions on unknown posts are skipped.

Hint 2

Use a set of follower ids per followee for the follower count, and a second set per (followee, month) for the monthly count, so repeated follow rows are not counted twice.

Hint 3

For the monthly report, group the (month, count) pairs by followee and sort them by month. Walk through them while keeping the previous count and a running total. The growth rate is None when there is no previous value.