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’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.
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.
event_type = 'Repl created'.entity_id and event_type.entity_id, keep exactly 1 Repl created event (the earliest by created_at, tie-break by smallest event_id).event_id, entity_id, event_type, created_at, ingested_at, and duplicate_rank (where rank 1 is the kept row, rank > 1 are duplicates).entity_id, then duplicate_rank, then created_at.| Column | Type | Description |
|---|---|---|
| event_idPK | BIGINT | Unique identifier for the event row; used as deterministic tie-breaker. |
| entity_id | VARCHAR(64) | Business entity identifier (e.g., transfer_id) whose lifecycle is being replicated. |
| event_type | VARCHAR(50) | Event name such as 'Repl created' or 'Repl updated'. |
| created_at | TIMESTAMP | Timestamp when the event occurred in the source system (event time). |
| ingested_at | TIMESTAMP | Timestamp when the event was ingested into the warehouse (arrival time). |
| source | VARCHAR(30) | Producing service identifier (e.g., replicator version). |