Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Deduplicate Out-of-Order Replication Events

MediumSQL · PostgreSQL00:00
I
Practice interviewer
Your interviewer
In session
I
Interviewer

Welcome to the SQL screen.

The question is on your right: Deduplicate Out-of-Order Replication Events. Read through the requirements and the one table first.

Run and submit your code as often as you need. You also have five interviewer messages this session - want to talk through your approach, or are you ready to start coding?

You need to log in / sign up to run or submit.

Problem

Business Context

You’re on the data platform team at a fintech payments company processing 50M+ ledger events/day from multiple upstream services (KYC, card processing, bank transfers). Events are ingested into a centralized warehouse for downstream reconciliation and regulatory reporting.

A recent incident revealed that the upstream “replication” service occasionally emits duplicate Repl created events for the same business entity (e.g., the same transfer), and these duplicates can arrive out of order due to retries, network delays, and at-least-once delivery semantics. If duplicates aren’t removed, downstream jobs may double-count “created” states, causing incorrect operational dashboards and noisy alerts.

Task

Write a SQL query that identifies duplicate Repl created events that may arrive out of order and outputs the rows that should be removed (i.e., all but the earliest true creation per entity).

Assume that for a given entity_id, the correct record to keep is the event with the earliest created_at timestamp. If multiple events tie on created_at, keep the one with the smallest event_id.

Requirements

  1. Consider only rows where event_type = 'Repl created'.
  2. Define duplicates as events with the same entity_id and event_type.
  3. For each entity_id, keep exactly 1 Repl created event (the earliest by created_at, tie-break by smallest event_id).
  4. Return the events that should be deleted, with columns: event_id, entity_id, event_type, created_at, ingested_at, and duplicate_rank (where rank 1 is the kept row, rank > 1 are duplicates).
  5. Order the output by entity_id, then duplicate_rank, then created_at.

Schema

replication_events
ColumnTypeDescription
event_idPKBIGINTUnique identifier for the event row; used as deterministic tie-breaker.
entity_idVARCHAR(64)Business entity identifier (e.g., transfer_id) whose lifecycle is being replicated.
event_typeVARCHAR(50)Event name such as 'Repl created' or 'Repl updated'.
created_atTIMESTAMPTimestamp when the event occurred in the source system (event time).
ingested_atTIMESTAMPTimestamp when the event was ingested into the warehouse (arrival time).
sourceVARCHAR(30)Producing service identifier (e.g., replicator version).
Tablesreplication_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results