Compute feed ad frequency and retention in SQL

Quick Overview

This question evaluates SQL-based product analytics skills, specifically session-level aggregation for ad frequency, impression-to-click joins for CTR by feed position bins, cohort-based retention computation, and user-fixed-effect adjustments; it is categorized under Data Manipulation (SQL/Python) for a Data Scientist role.

Compute feed ad frequency and retention in SQL

Company: Meta

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Onsite

Assume today is 2025-09-01. Schema and tiny samples: feed_impressions(impression_id, user_id, impression_time, content_type, feed_position, session_id) 100 | 1 | 2025-08-31 09:00 | organic | 1 | s1 101 | 1 | 2025-08-31 09:01 | ad | 2 | s1 102 | 1 | 2025-08-31 09:02 | organic | 3 | s1 103 | 1 | 2025-08-31 09:03 | ad | 4 | s1 200 | 3 | 2025-08-31 10:00 | organic | 1 | s2 201 | 3 | 2025-08-31 10:01 | ad | 2 | s2 feed_clicks(impression_id, click_time) 101 | 2025-08-31 09:01:10 103 | 2025-08-31 09:03:05 201 | 2025-08-31 10:01:05 sessions(session_id, user_id, session_start, session_end) s1 | 1 | 2025-08-31 09:00 | 2025-08-31 09:10 s2 | 3 | 2025-08-31 10:00 | 2025-08-31 10:05 user_activity(user_id, event_date) 1 | 2025-08-25 1 | 2025-09-01 3 | 2025-08-25 3 | 2025-08-29 Tasks: (a) For each session, compute ad_frequency = ads/(ads+organic) considering positions ≥2 only, and flag sessions where ad_frequency > 0.30. (b) Compute ad CTR by position bin (1–2, 3–5, 6+) using impressions joined to clicks, and also compute a user-fixed-effect adjusted CTR by subtracting each user’s mean CTR. (c) For the last 7 days (2025-08-26 to 2025-09-01), compute D1 and D7 retention for cohorts of users who saw ≥1 ad vs saw 0 ads, where cohorts are defined by each user’s first activity date in that window. Return a single result table with cohort_date, cohort_type (saw_ad/zero_ad), d1_retention, d7_retention.

Overview: This question evaluates SQL-based product analytics skills, specifically session-level aggregation for ad frequency, impression-to-click joins for CTR by feed position bins, cohort-based retention computation, and user-fixed-effect adjustments; it is categorized under Data Manipulation (SQL/Python) for a Data Scientist role.

Read the full Meta Data Scientist interview experience this question came from

Community answers

Answer by melodyyun.z

-- Task a, assuming impression_id is the primary key of table feed_impressions with session_feed as ( select session_id , count(impression_id) as feeds , sum(case when content_type = 'ad' then 1 else 0 end) as ads from feed_impressions where feed_position >= 2 and content_type in ('ad', 'organic') group by 1 ) select session_id , 1.0 * ads/feeds as ad_frequency , case when 1.0 * ads/feeds > 0.3 then true else false end as high_ad_frequency_session from session_feed -- Task b, assuming impression_id is the PK of table feed_clicks with position_bin_click as ( select f.impression_id , f.user_id , f.content_type , f.feed_position , case when f.feed_position in (1, 2) then '1-2' when f.feed_position in (3, 4, 5) then '3-5' when f.feed_position >= 6 then '6+' else 'unknown' end as position_bin , c.impression_id as c_impression_id , case when c.impression_id is NULL then 0.0 else 1.0 end as is_clicked from feed_impressions f left join feed_clicks c on f.impression_id = c.impression_id where f.content_type = 'ad' ) , user_mean_ctr as ( select user_id , avg(is_clicked) as mean_ctr from position_bin_click where position_bin != 'unknown' group by 1 ) select p.position_bin , 1.0 * sum(p.is_clicked)/count(p.impression_id) as ctr , 1.0 * avg(p.is_clicked - u.mean_ctr) as demeaned_ctr from position_bin_click p join user_mean_ctr u on p.user_id = u.user_id where p.position_bin != 'unknown' group by 1 -- Task c, problem description is ambiguous. Can not provide solution with clarifications.
Meta logo
Meta
Oct 13, 2025
mediumData ScientistOnsiteData Manipulation (SQL/Python)
11
0

Assume today is 2025-09-01. Schema and tiny samples:

feed_impressions(impression_id, user_id, impression_time, content_type, feed_position, session_id) 100 | 1 | 2025-08-31 09:00 | organic | 1 | s1 101 | 1 | 2025-08-31 09:01 | ad | 2 | s1 102 | 1 | 2025-08-31 09:02 | organic | 3 | s1 103 | 1 | 2025-08-31 09:03 | ad | 4 | s1 200 | 3 | 2025-08-31 10:00 | organic | 1 | s2 201 | 3 | 2025-08-31 10:01 | ad | 2 | s2

feed_clicks(impression_id, click_time) 101 | 2025-08-31 09:01:10 103 | 2025-08-31 09:03:05 201 | 2025-08-31 10:01:05

sessions(session_id, user_id, session_start, session_end) s1 | 1 | 2025-08-31 09:00 | 2025-08-31 09:10 s2 | 3 | 2025-08-31 10:00 | 2025-08-31 10:05

user_activity(user_id, event_date) 1 | 2025-08-25 1 | 2025-09-01 3 | 2025-08-25 3 | 2025-08-29

Tasks: (a) For each session, compute ad_frequency = ads/(ads+organic) considering positions ≥2 only, and flag sessions where ad_frequency > 0.30. (b) Compute ad CTR by position bin (1–2, 3–5, 6+) using impressions joined to clicks, and also compute a user-fixed-effect adjusted CTR by subtracting each user’s mean CTR. (c) For the last 7 days (2025-08-26 to 2025-09-01), compute D1 and D7 retention for cohorts of users who saw ≥1 ad vs saw 0 ads, where cohorts are defined by each user’s first activity date in that window. Return a single result table with cohort_date, cohort_type (saw_ad/zero_ad), d1_retention, d7_retention.