The filtering logic alone took me a while to think through clearly.
Break the problem into three phases: first, filter and classify users based on their activity within the two-month window, excluding any user with events in multiple categories or outside the allowed categories; second, generate a user-week spine and compute active days per user-week, handling partial weeks at the boundaries; third, aggregate per cohort to get user count, average weekly active days per user, and the fraction of user-weeks with at least 3 active days. Use CTEs and window functions to keep the logic modular and readable.
Pro tip: Explicitly state your assumptions about the data schema (e.g., event timestamps, category values) and clarify how you handle edge cases like users with no activity in a week or partial weeks; this shows you think about data quality and metric definitions, which is critical for product analytics roles.
Identify users who have events only in one allowed category (e.g., 'social' or 'game') within the two-month window, excluding any user with events in multiple categories or outside the allowed categories. Use a GROUP BY with HAVING COUNT(DISTINCT category) = 1 and filter for allowed categories.
Create a spine of all weeks in the window and join with user events to compute active days per user per week. For partial weeks at the boundaries, only count days that fall within the window. Use date functions to truncate event timestamps to week and count distinct active days.
For each user, calculate total active days, number of weeks active (or total weeks in window?), and average weekly active days. Also flag weeks where active days >= 3. Be careful to define 'weekly active days per user' as total active days divided by number of weeks in the window (or weeks with activity?)—clarify with the interviewer.
Group by cohort (social-only vs game-only) to compute: number of users (COUNT DISTINCT user_id), average weekly active days per user (AVG of per-user averages), and fraction of user-weeks with >=3 active days (SUM of weeks with >=3 / total user-weeks).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.