KatInterview

User Signup and Follower Metrics Report

SQL & DataAuto-graded · 15 test cases
MediumAuto-gradedSQL & Dataaggregationgroupingcounting

Last updated

Simulate three SQL analytics queries in Python: total users, busiest signup month (with tie-break), and the most-followed usernames including ties.

You are given a snapshot of data for a fictional social app, already loaded into Python instead of a database:

  • users: a list of dictionaries, each with keys "user_id" (int), "username" (str), and "created_at" (a timestamp string of the form "YYYY-MM-DD HH:MM:SS"). Each user_id appears at most once.
  • follows: a list of dictionaries, each with keys "follower_user_id" (int) and "followed_user_id" (int). A row means the follower follows the followed user. Rows may repeat, may reference IDs that do not exist in users, and may be self-follows (follower equals followed).

Write a function user_metrics(users, follows) that returns a dict with exactly these keys:

  1. "user_count": the number of rows in users.
  2. "busiest_month": the calendar month (as a string "YYYY-MM" obtained from the first seven characters of created_at) in which the largest number of users signed up. If several months tie for the largest count, choose the lexicographically smallest such month string. If users is empty, this is None.
  3. "busiest_month_count": the number of signups in "busiest_month". If users is empty, this is 0.
  4. "most_followed_usernames": a list, sorted in ascending lexicographic order, of the usernames of all users whose follower count equals the maximum follower count. A user's follower count is the number of follows rows whose followed_user_id equals that user's user_id (duplicates count separately). Users with 0 followers are eligible only when the table is non-empty and no one has any followers. If users is empty, this is an empty list.

Every user present in users is considered, including those with no followers. Follow rows pointing at IDs not in users are ignored for follower counting.

Examples

Example 1

Input: user_metrics([{'user_id': 1, 'username': 'amy', 'created_at': '2024-01-15 10:00:00'}, {'user_id': 2, 'username': 'ben', 'created_at': '2024-02-01 09:00:00'}, {'user_id': 3, 'username': 'cy', 'created_at': '2024-02-18 12:00:00'}], [{'follower_user_id': 1, 'followed_user_id': 2}, {'follower_user_id': 3, 'followed_user_id': 2}, {'follower_user_id': 2, 'followed_user_id': 1}])

Output: {'user_count': 3, 'busiest_month': '2024-02', 'busiest_month_count': 2, 'most_followed_usernames': ['ben']}

Example 2

Input: user_metrics([{'user_id': 1, 'username': 'zoe', 'created_at': '2023-05-01 00:00:00'}, {'user_id': 2, 'username': 'amy', 'created_at': '2024-05-01 00:00:00'}], [])

Output: {'user_count': 2, 'busiest_month': '2023-05', 'busiest_month_count': 1, 'most_followed_usernames': ['amy', 'zoe']}

Hints

Hint 1

Build a frequency dict from the first seven characters of each created_at string; the answer month is the key with the highest count, breaking ties by the smallest key.

Hint 2

Initialize every known user to a follower count of 0 before tallying, so users nobody follows still participate in the maximum.

Hint 3

When follows is empty, all users tie at zero followers, so every username should appear in sorted order.