KatInterview

First-Week Post Totals and Week-Later Return Share

Arrays & HashingAuto-graded · 23 test cases
MediumAuto-gradedArrays & Hashinghash-mapdatesaggregation

Last updated

Group dated activity rows by user, find each user's earliest date, total posts in the first seven days, and compute the share of users active on day 7 or later.

You are given an activity log for a posting platform. Each record is a list [user_id, posting_date, num_posts]:

  • user_id is an integer identifying the user.
  • posting_date is a string in YYYY-MM-DD format.
  • num_posts is a non-negative integer, the number of posts the user made on that date.

Rows may appear in any order, and a user may have several rows, including several rows on the same date.

Task

For each user, define their start date as the earliest posting_date among all of that user's rows. The start date is day 0, and a row's day offset is the number of calendar days between its date and the user's start date.

Compute:

  1. First-week posts for each user: the sum of num_posts over that user's rows with day offset from 0 to 6 inclusive.
  2. Return rate across all users: the fraction of distinct users who have at least one row with day offset 7 or greater, divided by the total number of distinct users.

A row counts as activity for a user even when its num_posts is 0. This applies both to determining the start date and to the day-7-or-later check.

Output

Return a dictionary with two keys:

  • "users": a list with one entry per distinct user, sorted by user_id ascending. Each entry is a dictionary with:
    • "user_id": the user's id,
    • "registration_date": the start date as a YYYY-MM-DD string,
    • "posts_first_7_days": the first-week post total.
  • "retention_rate": the return rate as an unrounded float. If the input is empty, return {"users": [], "retention_rate": 0.0}.

Constraints

  • 0 <= len(user_posts) <= 10^5
  • All dates are valid ISO dates (YYYY-MM-DD).
  • num_posts >= 0
  • Input rows are not guaranteed to be sorted.
  • Day 6 belongs to the first-week total and does not make a user retained; day 7 is excluded from the total and does make a user retained.

Examples

Example 1

Input: first_week_activity_summary([[1, '2023-01-01', 3], [1, '2023-01-02', 2], [2, '2023-02-10', 1], [2, '2023-02-15', 4], [3, '2023-03-05', 1]])

Output: {'users': [{'user_id': 1, 'registration_date': '2023-01-01', 'posts_first_7_days': 5}, {'user_id': 2, 'registration_date': '2023-02-10', 'posts_first_7_days': 5}, {'user_id': 3, 'registration_date': '2023-03-05', 'posts_first_7_days': 1}], 'retention_rate': 0}

Hints

Hint 1

Make one pass to find each user's earliest date. Do not assume the first row you see for a user is the earliest.

Hint 2

Once you have each user's start date, convert each row to a day offset using real date objects and handle each row by its offset: 0 to 6 adds to the total, 7 or more marks the user as returned.

Hint 3

Track returned users in a set so that many late rows still count a user only once, and divide by the number of distinct users (guarding against an empty input).