Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Deduplicate Out-of-Order Replication Events
00:00
5 left

Deduplicate Out-of-Order Replication Events

MediumSQL · PostgreSQL

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
Interviewer

Your question is Deduplicate Out-of-Order Replication Events. Start with the requirements and the one table in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.