Deduplicate Ad Impression Logs and Build a Frequency Distribution

Last updated: November 29, 2026

Quick Overview

Ad impression events are ingested with retries, so the same impression_id can appear several times. Write SQL that deduplicates them and returns, for one campaign and week, how many users saw the ad 1, 2, ..., 9, and 10+ times.

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

0

0

0 solved


Ad impression events are ingested with retries, so the same impression_id can appear several times. Write SQL that deduplicates them and returns, for one campaign and week, how many users saw the ad 1, 2, ..., 9, and 10+ times.

Frequency distributions drive Meta Ads frequency capping and reach reporting, and duplicate events from at-least-once pipelines are a daily reality for data engineers. The interviewer checks ROW_NUMBER deduplication, bucket construction, share computed with a window over aggregates, and correct ordering of buckets.

What the Interviewer Expects
  • Deduplicates by impression_id keeping one deterministic row
  • Filters to the campaign and half-open date range
  • Counts impressions per user and buckets the tail
  • Computes each bucket's share with a window over grouped counts
  • Orders buckets numerically rather than as strings
Key Topics to Cover
Deduplication
ROW_NUMBER
Frequency distribution
Bucketing
Window over aggregates
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
  • What if duplicates carry slightly different timestamps?
  • How would you make the deduplication incremental in a daily pipeline?
  • How would you compute average frequency and reach from the same table?
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

Table: impressions_raw(impression_id, user_id, campaign_id, event_ts, ingested_at). Retries create rows with the same impression_id and identical event fields but later ingested_at. Week: 2026-1...

Approach
  1. ROW_NUMBER() OVER (PARTITION BY impression_id ORDER BY ingested_at DESC) and keep row 1. Since duplicates share event fields, which copy we keep does not change counts, but a deterministic rule m...

Submit Your Answer
Markdown supported