Incrementally Maintain a Daily Cohort Retention Table With Late Data

Last updated: November 29, 2026

Quick Overview

Design the SQL for a daily pipeline that maintains a cohort retention table keyed by (signup_date, day_offset) for offsets 0 to 30, updating only the affected cells each day and reprocessing the last three days of activity to absorb late-arriving events.

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

0

0

0 solved


Design the SQL for a daily pipeline that maintains a cohort retention table keyed by (signup_date, day_offset) for offsets 0 to 30, updating only the affected cells each day and reprocessing the last three days of activity to absorb late-arriving events.

Meta's retention dashboards are backed by daily pipelines over enormous activity tables, so recomputing every cohort from scratch is not an option. This data engineering onsite question checks whether you can identify exactly which cells a day of new data touches, write an idempotent upsert, and handle late events without double counting.

What the Interviewer Expects
  • Recognize that activity on day d only affects cells where signup_date = d - k
  • Compute cell values from distinct users, not by adding increments
  • Make the job idempotent so reruns produce the same result
  • Reprocess a trailing window for late data
  • Keep cohort size in sync with the users table
Key Topics to Cover
incremental processing
idempotency
MERGE
late-arriving data
partitioning
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
  • Why is adding daily increments to existing counts unsafe here?
  • How would you partition this table in a Hive-style warehouse without MERGE?
  • How would you backfill after a definition change?
  • How would you monitor that the table is correct?
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

Sources: users(user_id, signup_date) and daily_active(user_id, ds) partitioned by ds, where events for day d can arrive up to 2 days late. Target: `cohort_retention(signup_date, day_offset, coho...

Approach

For each activity day ds in the window, the affected cells are (ds - k, k) for k in 0..30, one diagonal of the triangle. Recompute each affected cell from scratch as a distinct count of users of coh...


Submit Your Answer
Markdown supported