The session window matching part is what trips you up if you're not careful.
Start by defining the date range and generating a complete date spine to ensure all dates are represented. Then, for each date, compute the distinct counts of active users, login users, and valid purchase users by joining sessions and events appropriately. Finally, calculate the conversion rates as decimals, handling division by zero.
Pro tip: Use a date spine to avoid missing dates with zero activity, and be explicit about how you handle sessions that span midnight—decide whether to attribute events to the session start date or the event date, and document your choice.
Create a series of dates covering the entire range to ensure every date appears in the output, even if there is no activity.
Count distinct user_id from sessions (or events) for each date, depending on the definition of 'active'.
Count distinct user_id from events where event_type = 'login' for each date.
Join events (purchases) with sessions on user_id and session_id, ensuring the event timestamp falls within the session's start and end time; count distinct user_id per date.
Compute login-to-purchase as valid_purchase_users / login_users and session-to-purchase as valid_purchase_users / active_users, using NULLIF to avoid division by zero.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Use a CTE to compute each user's first login and first purchase dates via MIN aggregation, then join these results and apply a DATEDIFF check to flag whether the first purchase occurred within 7 days inclusive of the first login. Ensure the logic mirrors the valid-session-window approach by using inclusive boundaries and handling users with no purchases.
Pro tip: Explicitly state that you're using an inclusive 7-day window (i.e., purchase_date <= login_date + 7 days) and mention how you'd handle users with no purchases—this shows attention to edge cases and metric definition rigor, which is critical at Coinbase.
Write a CTE that selects user_id and MIN(login_date) as first_login_date from the logins table, grouping by user_id.
Write a second CTE that selects user_id and MIN(purchase_date) as first_purchase_date from the purchases table, grouping by user_id.
LEFT JOIN the two CTEs on user_id to retain users who logged in but never purchased, ensuring first_purchase_date is NULL for them.
Use a CASE expression with DATEDIFF(day, first_login_date, first_purchase_date) BETWEEN 0 AND 7 to flag whether the first purchase happened within 7 days inclusive of the first login.
Select user_id, first_login_date, first_purchase_date, and the flag; optionally order by user_id for readability.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
The documentation requirement is the part I spent the most time on.
First, clarify the definitions of 'valid event', 'first login', and how to handle duplicates and orphaned events. Then, outline a pandas-based solution: filter users by first login date in the 7-day window, compute their first login date, and check for any valid event exactly one day later. Finally, discuss edge cases and how to document your assumptions.
Pro tip: Mention that you would validate the retention calculation by manually checking a few user journeys, and that you would consider using a left join to ensure all users in the cohort are included, even those without a day-1 event.
Define what constitutes a 'valid event', 'first login', and how to handle duplicates and orphaned events. State assumptions explicitly, such as whether orphaned events are ignored or cause errors.
From the events data, filter to login events, drop duplicates to get unique user-date pairs, then compute the first login date per user. Then select users whose first login falls within the 7-day window ending on as_of.
For each user in the cohort, check if they have any valid event exactly one calendar day after their first login. Use a merge or groupby to flag these users.
Compute the percentage as the number of retained users divided by the total number of users in the cohort, multiplied by 100. Handle division by zero if the cohort is empty.
Explain how duplicates are removed (e.g., drop_duplicates on user_id and event_date) and how orphaned events (events without a corresponding user or login) are treated (e.g., ignored or logged).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.