KatInterview

Cohort Engagement Metrics for Single-Category Users

SQL & DataModel answer
MediumModel answerSQL & Datacohort-analysisaggregationwindow-functions

Last updated

Filter users into mutually exclusive content-type cohorts, then compute activity counts, median active days, a 10-day threshold rate, and per-user average minutes.

Cohort Engagement Metrics for Single-Category Users

You have a product with two content types that interest you: "social" and "game". Your goal is to compare how deeply each type's dedicated audience engages with the platform.

Schema

users(user_id INT, signup_date DATE)

sessions(session_id INT,
         user_id INT,
         session_start TIMESTAMP,
         session_end TIMESTAMP,
         category VARCHAR,
         duration_minutes INT)

Analysis window

Include only sessions where session_start falls on or between 2025-08-04 and 2025-09-01, inclusive.

Data quality rules

Before any analysis, discard sessions that have a NULL category or a duration_minutes value that is zero or negative.

Cohort definitions

Using only the cleaned, in-window sessions, classify each user into exactly one of two groups:

  • social_only — at least one 'social' session and zero 'game' sessions
  • game_only — at least one 'game' session and zero 'social' sessions

Drop everyone else: users who appear in both categories, and users who only have sessions in other categories.

Metrics to compute (per cohort)

ColumnDefinition
users_countNumber of users in the cohort
median_active_daysMedian (across users) of each user's count of distinct calendar days that had at least one session
pct_users_active_10_plus_daysPercentage (0–100 scale) of users in the cohort whose active-day count is ≥ 10
avg_minutes_per_active_dayFor each user, divide their total duration_minutes by their active-day count; then average that ratio across users in the cohort

Output

Return one row per cohort with the columns above, ordered by cohort.

cohort | users_count | median_active_days | pct_users_active_10_plus_days | avg_minutes_per_active_day

Sample data

users

user_idsignup_date
12025-07-15
22025-08-10
32025-08-20
42025-08-25

sessions

session_iduser_idsession_startsession_endcategoryduration_minutes
10112025-08-05 10:00:002025-08-05 10:30:00social30
10212025-08-06 10:00:002025-08-06 10:20:00social20
10322025-08-07 09:00:002025-08-07 10:00:00game60
10422025-08-08 09:00:002025-08-08 09:45:00game45
10532025-08-09 12:00:002025-08-09 12:10:00social10
10632025-08-13 12:00:002025-08-13 12:10:00game10
10742025-08-11 14:00:002025-08-11 14:30:00game30

User 3 has both a social and a game session, so they are excluded. The expected output from the sample is:

cohortusers_countmedian_active_dayspct_users_active_10_plus_daysavg_minutes_per_active_day
game_only21.50.0041.25
social_only12.00.0025.00

Write a single PostgreSQL query (CTEs are fine) that produces this result for the given window.

Hints

Hint 1

Build the query in three layers: clean and restrict to the window first, roll up to one row per user second, assign cohorts and aggregate third. Classifying users before cleaning is a common mistake — a session outside the window should not disqualify someone from social_only.

Hint 2

For the date boundary, avoid BETWEEN on a TIMESTAMP column. '2025-09-01' casts to midnight, silently dropping everything that happened during the rest of that day. Use `session_start >= start AND session_start < start + INTERVAL '1 day'` applied to the day after your end date instead.

Hint 3

avg_minutes_per_active_day is the mean of a per-user ratio, not a global ratio. AVG(total_minutes / active_days) and SUM(total_minutes) / SUM(active_days) give different numbers; the spec asks for the first form. Also watch out for integer division: cast the numerator or denominator to NUMERIC before dividing.