KatInterview

Variant-Level Tutorial Finish Rate Within a One-Week Window

SQL & DataAuto-graded · 17 test cases
MediumAuto-gradedSQL & Datahash-mapaggregationdatetime

Last updated

Join users to step events, cap each user's progress at a 7-day window, infer each variant's final step, and compute per-variant completion shares.

Tutorial Finish Rate per Variant

A product team is testing two onboarding tutorial designs. Each user is assigned one design (tutorial_variant) at sign-up. As users work through the tutorial, each step they reach is logged as an event. You need to measure how many users of each design finish the tutorial in their first week.

Input

  • users: a list of dictionaries, each with
    • user_id: the user's identifier,
    • sign_up_at: an ISO-8601 string, either a plain date such as "2023-08-01" (meaning midnight at the start of that day) or a full date-time,
    • tutorial_variant: a string naming the assigned design.
  • events: a list of dictionaries, each with
    • event_id: the event identifier,
    • user_id: the user who triggered it,
    • step_index: an integer, the tutorial step reached,
    • event_time: an ISO-8601 date-time string.

A trailing Z in any timestamp means UTC and should be treated as +00:00. All timestamps in a given test are either all timezone-aware or all naive, so they can be compared directly.

Definitions

  1. Window. An event counts for a user only if event_time <= sign_up_at + 7 days. The upper bound is inclusive. There is no lower bound, so events stamped before sign-up still count.
  2. User progress. A user's progress is the largest step_index among their counted events. A user with no counted events has progress 0.
  3. Variant finish line. For each variant, the finish line is the largest progress value among all users assigned to that variant.
  4. Completed. A user has completed the tutorial if their progress equals their variant's finish line and is greater than 0.
  5. Completion rate. For each variant, the number of completed users divided by the total number of users assigned to that variant. This includes users who never produced a counted event.

Events whose user_id does not appear in users are ignored.

Output

Return a list with one dictionary per variant present in users, sorted by variant name in ascending order. Each dictionary has the form:

{"tutorial_variant": <str>, "completion_rate": <number>}

The rate is a plain, unrounded number between 0 and 1.

Constraints

  • 0 <= len(users), and user_id values in users are unique.
  • 0 <= len(events).
  • step_index is a non-negative integer.
  • Events may arrive in any order and may contain duplicates.
  • A variant whose users all have progress 0 has a completion rate of 0.
  • If users is empty, return an empty list.

Examples

Example 1

Input: variant_completion_rates([{'user_id': 101, 'sign_up_at': '2023-08-01', 'tutorial_variant': 'original'}, {'user_id': 102, 'sign_up_at': '2023-08-02', 'tutorial_variant': 'variant'}, {'user_id': 103, 'sign_up_at': '2023-08-02', 'tutorial_variant': 'original'}, {'user_id': 104, 'sign_up_at': '2023-08-03', 'tutorial_variant': 'variant'}, {'user_id': 105, 'sign_up_at': '2023-08-03', 'tutorial_variant': 'original'}], [{'event_id': 1, 'user_id': 101, 'step_index': 1, 'event_time': '2023-08-01T10:00:00'}, {'event_id': 2, 'user_id': 101, 'step_index': 2, 'event_time': '2023-08-01T10:02:00'}, {'event_id': 3, 'user_id': 102, 'step_index': 1, 'event_time': '2023-08-02T11:00:00'}, {'event_id': 4, 'user_id': 104, 'step_index': 1, 'event_time': '2023-08-03T09:00:00'}, {'event_id': 5, 'user_id': 105, 'step_index': 1, 'event_time': '2023-08-03T12:00:00'}])

Output: [{'tutorial_variant': 'original', 'completion_rate': 0.3333333333333333}, {'tutorial_variant': 'variant', 'completion_rate': 1}]

Example 2

Input: variant_completion_rates([{'user_id': 1, 'sign_up_at': '2024-01-01', 'tutorial_variant': 'original'}, {'user_id': 2, 'sign_up_at': '2024-01-01', 'tutorial_variant': 'variant'}], [{'event_id': 1, 'user_id': 1, 'step_index': 1, 'event_time': '2024-01-01T10:00:00'}, {'event_id': 2, 'user_id': 1, 'step_index': 2, 'event_time': '2024-01-02T10:00:00'}, {'event_id': 3, 'user_id': 2, 'step_index': 1, 'event_time': '2024-01-01T10:00:00'}])

Output: [{'tutorial_variant': 'original', 'completion_rate': 1}, {'tutorial_variant': 'variant', 'completion_rate': 1}]

Hints

Hint 1

Build one record per user first (sign-up time, variant, best step so far, starting at 0), so users with no events are still counted in the denominator.

Hint 2

Scan the events once: skip unknown users, and only update a user's best step when the event time is at or before sign-up plus 7 days.

Hint 3

After per-user progress is known, compute each variant's maximum progress, then count users whose progress equals it and is above 0. Divide by the variant's total user count and sort by variant name.