Deduplicate Order Status Events to the Latest State per Order
Last updated: November 29, 2026
Quick Overview
Given an order_status_events table fed by an at-least-once stream (order_id, status, event_ts, ingested_at), return one row per order with its current status. Duplicate deliveries of the same event and late-arriving events must not distort the answer.
Amazon
November 29, 20260
0
0 solved
Given an order_status_events table fed by an at-least-once stream (order_id, status, event_ts, ingested_at), return one row per order with its current status. Duplicate deliveries of the same event and late-arriving events must not distort the answer.
Amazon order pipelines emit status changes through queues like Kinesis or SQS that guarantee at-least-once delivery, so duplicates are normal. Phone screens for data roles often open with this to check that the candidate reaches for a window function, picks a deterministic tie-breaker, and orders by event time instead of arrival time.
What the Interviewer Expects
- Use ROW_NUMBER partitioned by order_id rather than GROUP BY with MAX on a non-key column
- Order by business event time first, ingestion time second, so late arrivals do not win incorrectly
- Explain why the tie-breaker makes the result deterministic across reruns
- Return exactly one row per order and state that assumption
- Mention how the same pattern applies to incremental loads (MERGE into a current-state table)
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 change the query if two different statuses share the exact same event_ts?
- How would you maintain this as an incremental daily job instead of a full scan?
- What changes if a status can legitimately move backward, for example DELIVERED to RETURN_REQUESTED?
- How would you measure the duplicate rate of the stream itself?
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
order_status_events(order_id, status, event_ts, ingested_at). One logical event can appear several times because the producer retries.event_tsis when the status changed in the order system; ...
Approach
A common wrong answer is SELECT order_id, MAX(event_ts), status ... GROUP BY order_id, which either fails or returns an arbitrary status depending on the engine. The correct tool is a window functio...