Deduplicate Double-Fired Impression Events From Client Retries

Last updated: November 29, 2026

Quick Overview

The impressions log contains duplicate rows because mobile clients retry uploads. Write a query that keeps exactly one row per event_id, preferring the most recently ingested copy, and report how many duplicates were removed.

TikTok
Data Manipulation (SQL/Python)
Data Engineer
TikTok
November 29, 2026
Data Engineer
Phone Screen
Data Manipulation (SQL/Python)
Easy

0

0

0 solved


The impressions log contains duplicate rows because mobile clients retry uploads. Write a query that keeps exactly one row per event_id, preferring the most recently ingested copy, and report how many duplicates were removed.

TikTok's mobile clients batch and retry event uploads on flaky networks, so raw impression tables often contain exact or near-exact duplicates. This screen question checks whether you dedupe on the right key with a deterministic tiebreaker and quantify the impact before downstream metrics use the table.

What the Interviewer Expects
  • Identify event_id as the idempotency key, not the whole row
  • Use ROW_NUMBER with a deterministic ORDER BY tiebreaker
  • Explain why SELECT DISTINCT is not enough
  • Report duplicates removed as a data-quality metric
  • Mention where in the pipeline dedup should live
Key Topics to Cover
deduplication
ROW_NUMBER
idempotency key
data quality
client retries
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 some clients never set event_id and you must dedupe on user, video and a time tolerance?
  • How would you dedupe in a streaming job with late-arriving copies?
  • How would you alert if the duplicate rate suddenly jumps?
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

raw_impressions(event_id, user_id, video_id, event_ts, ingest_ts, client_version).

  • event_id is generated on the device once per impression, so retries reuse it.
  • Copies can differ slightly (a ...
Approach

SELECT DISTINCT * fails because retried copies differ in ingest_ts. Instead, number copies within each event_id by ingest_ts DESC and keep row 1. Add a second sort key so reruns of the job pro...


Submit Your Answer
Markdown supported