Deduplicate Retried Pangle Ad Logs Before Computing Daily Campaign CTR
Last updated: November 29, 2026
Quick Overview
Given an impressions table and a clicks table where client retries create duplicate rows in both, write SQL that returns daily click-through rate per campaign, counting each impression and each clicked impression once.
ByteDance
November 29, 20260
0
0 solved
Given an impressions table and a clicks table where client retries create duplicate rows in both, write SQL that returns daily click-through rate per campaign, counting each impression and each clicked impression once.
A warm-up SQL question in a ByteDance ads data science screen, framed around Pangle or TikTok ads logging where SDK retries and at-least-once delivery produce duplicate events. The interviewer checks whether you define the grain before aggregating and avoid the join fan-out that inflates CTR.
What the Interviewer Expects
- Asks what uniquely identifies an impression and a click before writing SQL
- Deduplicates both tables to the impression grain before joining
- Uses a LEFT JOIN so campaigns with zero clicks still appear
- Explains why COUNT(*) after a raw join would overstate both numerator and denominator
- Handles orphan clicks and clicks timestamped before their impression
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you attribute a click to the day of the impression versus the day of the click, and which does the ads team want?
- What if impression_id can be reused across campaigns after a bug?
- How would you restrict clicks to a 24 hour attribution window?
- How would you monitor the duplicate rate itself as a data quality metric?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
Tables: ad_impressions(impression_id, campaign_id, user_id, ts) and ad_clicks(click_id, impression_id, ts). I confirm that impression_id is the true key and that retries resend the same id with ...
Approach
- Collapse impressions to one row per
impression_id, keeping the earliest timestamp. - Collapse clicks to the set of distinct impression ids that were clicked, dropping clicks whose impression is ...