Variant-Level Tutorial Finish Rate Within a One-Week Window
SQL & DataAuto-graded · 17 test casesLast 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 withuser_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 withevent_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
- 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. - User progress. A user's progress is the largest
step_indexamong their counted events. A user with no counted events has progress0. - Variant finish line. For each variant, the finish line is the largest progress value among all users assigned to that variant.
- Completed. A user has completed the tutorial if their progress equals their variant's finish line and is greater than
0. - 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), anduser_idvalues inusersare unique.0 <= len(events).step_indexis a non-negative integer.- Events may arrive in any order and may contain duplicates.
- A variant whose users all have progress
0has a completion rate of0. - If
usersis 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The task has three stages.
- Per-user progress. Create a record for every user, starting with progress
0. Then scan the events once. For each event belonging to a known user, check whether its time is at or beforesign_up_at + 7 days. If so, raise that user's progress tostep_indexwhen it is larger. Because only a maximum is kept, duplicates and out-of-order events do not matter. - Finish line per variant. For each variant, take the maximum progress across its users. This is the step that counts as "finishing" for that variant.
- Aggregate. Count the users in each variant, and count those whose progress equals the finish line and is above
0. The rate iscompleted / total. Output the variants in sorted order.
Why it is correct
- Every user gets a record up front, so users with no counted events remain in the denominator. This is the same effect as a
LEFT JOINwith the time condition in theONclause. - The window check is inclusive and is applied per user relative to their own sign-up time, so a user finishing after the window is treated as not having reached that step.
- The finish line is derived from the same in-window progress values, so it matches the definition exactly.
- The
> 0guard prevents a variant where nobody has progressed from counting everyone as a completer, since0 == 0would otherwise hold. Such a variant yields a rate of0. - Events for unknown users are skipped, and events before sign-up are counted because no lower bound is applied.
Complexity
- Time: O(U + E + V log V), where U is the number of users, E the number of events, and V the number of variants (the log term comes from sorting variant names).
- Space: O(U + V) for the per-user records and per-variant aggregates.
Reference solution
from typing import List, Dict
from datetime import datetime, timedelta
def variant_completion_rates(users: List[Dict], events: List[Dict]) -> List[Dict]:
def parse(ts: str) -> datetime:
return datetime.fromisoformat(ts.replace('Z', '+00:00'))
# One record per user: sign-up time, variant, best in-window step
info = {}
for u in users:
info[u['user_id']] = {
'start': parse(u['sign_up_at']),
'variant': u['tutorial_variant'],
'best': 0,
}
window = timedelta(days=7)
for ev in events:
rec = info.get(ev['user_id'])
if rec is None:
continue
if parse(ev['event_time']) <= rec['start'] + window:
if ev['step_index'] > rec['best']:
rec['best'] = ev['step_index']
# Finish line per variant
finish = {}
for rec in info.values():
v = rec['variant']
finish[v] = max(finish.get(v, 0), rec['best'])
# Totals and completers per variant
totals = {}
done = {}
for rec in info.values():
v = rec['variant']
totals[v] = totals.get(v, 0) + 1
if rec['best'] > 0 and rec['best'] == finish[v]:
done[v] = done.get(v, 0) + 1
return [
{
'tutorial_variant': v,
'completion_rate': done.get(v, 0) / totals[v],
}
for v in sorted(totals)
]