Flag Accidental Duplicate Orders With Identical Carts Within Two Minutes

Last updated: November 29, 2026

Quick Overview

Using orders (order_id, customer_id, order_ts) and order_items (order_id, asin, qty), flag orders that repeat the same customer's previous order with an identical cart (same ASINs and quantities, in any line order) placed within two minutes, and return each suspected duplicate with the order it duplicates.

Amazon
Data Manipulation (SQL/Python)
Data Engineer
Amazon
November 29, 2026
Data Engineer
Onsite
Data Manipulation (SQL/Python)
Hard

0

0

0 solved


Using orders (order_id, customer_id, order_ts) and order_items (order_id, asin, qty), flag orders that repeat the same customer's previous order with an identical cart (same ASINs and quantities, in any line order) placed within two minutes, and return each suspected duplicate with the order it duplicates.

Double submits from flaky mobile connections or repeated taps on Buy Now create duplicate orders that customer service has to cancel, and Amazon's checkout teams monitor the rate. The question tests building an order-independent cart signature, deduplication with LAG over the right partition, and careful thinking about chains of duplicates.

What the Interviewer Expects
  • Build a canonical cart signature with an ordered string aggregation including quantities
  • Partition by customer and signature so only identical carts are compared
  • Use LAG to compare each order with the previous identical one rather than a quadratic self-join
  • Explain how chains (three rapid repeats) are reported
  • Use an inclusive two-minute bound and justify it
Key Topics to Cover
deduplication
STRING_AGG
LAG
cart signature
time windows
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
  • Should a chain of orders 90 seconds apart count as duplicates of the first order or of each other?
  • How would you also catch duplicates where one cart has an extra low-value item?
  • How would you compute the duplicate rate by app version to find the bug?
  • How would you make the signature robust if the same ASIN appears on two lines?
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
  • orders(order_id, customer_id, order_ts) and order_items(order_id, asin, qty); line order is arbitrary.
  • Identical cart means the same multiset of (ASIN, quantity). An order with B1 x1 and B2 x2...
Approach
  1. cart_sig: per order, STRING_AGG(asin:qty ORDER BY asin). Sorting inside the aggregate makes the signature independent of line order.
  2. LAG over `PARTITION BY customer_id, cart_signature ORDE...

Submit Your Answer
Markdown supported