Trailing 28-Day Distinct Facebook Groups Engaged per User

Last updated: November 29, 2026

Quick Overview

Given group engagement events (user_id, group_id, date), compute for every user and every calendar day in a range the number of distinct groups the user engaged with in the trailing 28 days (including that day), without materializing a user by day by group cross product.

Meta
Data Manipulation (SQL/Python)
Data Scientist
Meta
November 29, 2026
Data Scientist
Onsite
Data Manipulation (SQL/Python)
Hard

0

0

0 solved


Given group engagement events (user_id, group_id, date), compute for every user and every calendar day in a range the number of distinct groups the user engaged with in the trailing 28 days (including that day), without materializing a user by day by group cross product.

Facebook Groups tracks how many communities each person is actively part of, and a rolling distinct count is tricky because distincts are not additive. In an onsite round, the interviewer looks for the coverage-interval insight: each engagement day makes a group count for the next 28 days, so merging those intervals and sweeping gives exact distinct counts.

What the Interviewer Expects
  • Explain why rolling sums of daily distinct counts double count
  • Turn each (user, group, active day) into a coverage interval [d, d + 27]
  • Merge overlapping coverage intervals per user and group
  • Sweep +1/-1 events per user over days and cumsum
  • Densify only the user by day output, not by group
Key Topics to Cover
rolling distinct count
interval merging
sweep line
cumsum
Groups engagement
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you approximate this at Meta scale with HyperLogLog sketches?
  • How would you change it to count groups with at least 3 active days in the window?
  • How would you maintain it incrementally each day?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

Dates are calendar days. The window on day x is [x - 27, x]. Output covers every day from start to end for every user with any event, including days with count 0. Multiple events by the same user,...

Approach

A group counts for user u on day x if u engaged with it on any day in [x - 27, x], which is the same as x lying in some interval [d, d + 27] for an engagement day d. So: dedupe (user, group, day), cre...


Submit Your Answer
Markdown supported