Running Cumulative Count of Distinct Advertisers by Day

Last updated: November 29, 2026

Quick Overview

Given an ad spend log (date, advertiser_id, spend), return for each calendar day the number of advertisers who spent for the first time that day, the cumulative number of distinct advertisers ever seen, and the distinct advertisers active that day.

Meta
Data Manipulation (SQL/Python)
Data Scientist
Meta
November 29, 2026
Data Scientist
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


Given an ad spend log (date, advertiser_id, spend), return for each calendar day the number of advertisers who spent for the first time that day, the cumulative number of distinct advertisers ever seen, and the distinct advertisers active that day.

Growth questions about Meta's advertiser base (how many small businesses have ever advertised) need a cumulative distinct count, which cannot be built by summing daily distincts. The interviewer checks that you reduce to first-seen dates and keep days with no new advertisers in the series.

What the Interviewer Expects
  • Explain why cumulative sum of daily distinct counts double counts
  • Compute each advertiser's first spend date
  • Count new advertisers per day and cumsum
  • Reindex to every calendar day so the cumulative line has no gaps
  • Report daily active distinct advertisers alongside for context
Key Topics to Cover
cumulative distinct
first-seen date
cumsum
reindex
advertiser growth
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 compute cumulative distinct advertisers per country, where an advertiser can change country?
  • How would you approximate this with HyperLogLog at Meta scale?
  • How would you show advertisers who have not spent in 90 days as churned?
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

spend(date, advertiser_id, spend); zero-spend rows should not count as active, so I filter spend > 0. Output columns: date, active, new, cumulative. Every day between the first and last date...

Approach

Filter to positive spend. Daily active is groupby(date).advertiser_id.nunique(). For new advertisers, take groupby(advertiser_id).date.min() to get first spend dates, then count per date. Build a ...


Submit Your Answer
Markdown supported