KatInterview

First Long Run of Daily Activity per User

Arrays & HashingAuto-graded · 16 test cases
MediumAuto-gradedArrays & Hashinghashingdatesgaps-and-islands

Last updated

Group activity logs by user, dedupe dates, find maximal consecutive-day runs of length 7 or more, and report each user's earliest such run.

First Long Run of Daily Activity per User

You have a list of activity log entries from a platform. Each entry is a dictionary with two keys:

  • "user_id": identifies the user.
  • "active_date": a calendar date as an ISO string, "YYYY-MM-DD".

A user can have several entries on the same date.

Task

Implement first_long_run(records). For every user, find their earliest run of at least 7 consecutive calendar days on which they were active. Return one result dictionary per qualifying user.

Definitions and rules

  • A user is active on a date if at least one entry for that user has that date. Several entries on one date count as a single active day.
  • A run is a maximal block of consecutive calendar days that are all active days for the user. It cannot be extended by the day before its first date or the day after its last date. Use real calendar arithmetic, so month, year and leap-day boundaries (for example Feb 28, Feb 29, Mar 1) continue a run.
  • A run qualifies if it covers 7 or more days.
  • If a qualifying run is longer than 7 days, report the full run: its first and last dates, not just the first seven days.
  • If a user has several qualifying runs, report only the one with the earliest start date. Shorter runs that come earlier do not matter, because only qualifying runs are compared.
  • Users with no qualifying run do not appear in the output.
  • Ignore any entry where "user_id" or "active_date" is missing or None.

Output

Return a list of dictionaries, one per qualifying user, each with exactly these keys:

  • "user_id": the user's id, with the same value and type as in the input.
  • "start_date": the first date of the run, as "YYYY-MM-DD".
  • "end_date": the last date of the run, as "YYYY-MM-DD".

Sort the list in ascending order of "user_id", comparing the values as given in the input. If the ids are strings, the comparison is lexicographic, so "10" comes before "2". If there are no qualifying users, return an empty list.

Constraints

  • 0 <= len(records) <= 10^5
  • All dates are valid ISO dates (YYYY-MM-DD).
  • All user_id values in one call share the same type and can be compared with each other.
  • Entries may appear in any order.

Examples

Example 1

Input: first_long_run([{'user_id': 1, 'active_date': '2024-01-01'}, {'user_id': 1, 'active_date': '2024-01-01'}, {'user_id': 1, 'active_date': '2024-01-02'}, {'user_id': 1, 'active_date': '2024-01-03'}, {'user_id': 1, 'active_date': '2024-01-04'}, {'user_id': 1, 'active_date': '2024-01-05'}, {'user_id': 1, 'active_date': '2024-01-06'}, {'user_id': 1, 'active_date': '2024-01-07'}, {'user_id': 1, 'active_date': '2024-01-08'}, {'user_id': 1, 'active_date': '2024-02-01'}, {'user_id': 2, 'active_date': '2024-01-01'}, {'user_id': 2, 'active_date': '2024-01-02'}, {'user_id': 2, 'active_date': '2024-01-03'}, {'user_id': 3, 'active_date': '2024-03-01'}, {'user_id': 3, 'active_date': '2024-03-02'}, {'user_id': 3, 'active_date': '2024-03-03'}, {'user_id': 3, 'active_date': '2024-03-04'}, {'user_id': 3, 'active_date': '2024-03-05'}, {'user_id': 3, 'active_date': '2024-03-06'}, {'user_id': 3, 'active_date': '2024-03-07'}])

Output: [{'user_id': 1, 'start_date': '2024-01-01', 'end_date': '2024-01-08'}, {'user_id': 3, 'start_date': '2024-03-01', 'end_date': '2024-03-07'}]

Hints

Hint 1

Collapse duplicates first: for each user, keep a set of distinct dates. Two entries on the same day must not count as two days.

Hint 2

Sort each user's distinct dates, then scan them. A new run begins whenever a date is not exactly one day after the previous one.

Hint 3

Close a run when it ends and check its length (last - first) in days >= 6. Remember to close the final run after the loop, and keep only the first qualifying run per user since runs are found in date order.