cswork / libraryOpen practice workspace

Coding questions · Amazon

Identify Key Metrics for Monitoring Shipment Defects

This question evaluates a data scientist's skill in defining and computing operational monitoring metrics for shipment defects, emphasizing data manipulation, aggregation, and metric definition using SQL and Python.

shipment

+-------------+----------+------------+---------+---------+
| shipment_id | order_id | ship_date  | carrier | status  |
+-------------+----------+------------+---------+---------+
| 1001        | 5001     | 2023-07-01 | UPS     | shipped |
| 1002        | 5002     | 2023-07-02 | FedEx   | shipped |
| 1003        | 5003     | 2023-07-03 | DHL     | delayed |
+-------------+----------+------------+---------+---------+

defect

+-----------+-------------+--------------+--------------+
| defect_id | shipment_id | defect_type  | detected_date|
+-----------+-------------+--------------+--------------+
| 9001      | 1001        | broken_item  | 2023-07-05   |
| 9002      | 1003        | missing_part | 2023-07-06   |
| 9003      | 1003        | late_ship    | 2023-07-07   |
+-----------+-------------+--------------+--------------+
Scenario

Amazon logistics team wants to monitor product quality by tracking defects related to customer shipments.

Question

Which metrics would you define to monitor shipment defects on an ongoing basis? Write the SQL that calculates, for the last 30 days, each carrier’s total shipments, total defective shipments, and defect_rate (= defective/total). Return the top-3 carriers with the highest defect_rate.

Hints

Join shipment and defect, filter on ship_date or detected_date, GROUP BY carrier, use COUNT(DISTINCT) or COUNT(*) as appropriate; compute defect_rate in a CTE or HAVING clause.

Top carriers by shipment defect rate over the last 30 days

You are given two tables, shipment and defect. The logistics team wants to monitor product quality by tracking defects related to customer shipments.

Define metrics to monitor shipment defects and write a SQL query that, for shipments with ship_date between '2025-05-03' and '2025-06-01' (inclusive), returns for each carrier:

  • total_shipments
  • total_defective_shipments (shipments that have at least one associated defect)
  • defect_rate = total_defective_shipments / total_shipments

Return the top 3 carriers with the highest defect_rate, ordering by defect_rate descending, then by total_shipments descending, then by carrier ascending to break ties.

Table shipment

columntypeconstraints
shipment_idINTEGERPRIMARY KEY, NOT NULL
order_idINTEGERNOT NULL
ship_dateDATENOT NULL
carrierVARCHAR(50)NOT NULL
statusVARCHAR(20)NOT NULL

Sample data:

carrierorder_idship_dateshipment_idstatus
UPS50012025-05-101001shipped
FedEx50022025-05-151002shipped
DHL50032025-05-201003delayed

Table defect

columntypeconstraints
defect_idINTEGERPRIMARY KEY, NOT NULL
shipment_idINTEGERNOT NULL, FOREIGN KEY REFERENCES shipment(shipment_id)
defect_typeVARCHAR(50)NOT NULL
detected_dateDATENOT NULL

Sample data:

defect_iddefect_typedetected_dateshipment_id
9001broken_item2025-05-181001
9002missing_part2025-05-221003
9003late_ship2025-05-251003

Expected output

For shipments with ship_date between '2025-05-03' and '2025-06-01' (inclusive), return one row per carrier with columns: carrier, total_shipments, total_defective_shipments (shipments that have at least one associated defect), and defect_rate = total_defective_shipments / total_shipments. Order by defect_rate descending, then total_shipments descending, then carrier ascending, and return the top 3 carriers.

carrierdefect_ratetotal_defective_shipmentstotal_shipments
DHL111
UPS111
FedEx001

Hints

  1. Filter shipments to the last 30 days explicitly, e.g., ship_date BETWEEN '2025-05-03' AND '2025-06-01'.
  2. LEFT JOIN shipment to defect so that non-defective shipments are still counted in total_shipments.
  3. Use COUNT(DISTINCT shipment_id) on the defect side (or a pre-aggregated CTE) to avoid double-counting shipments with multiple defects.
  4. Group by carrier and compute defect_rate as total_defective_shipments * 1.0 / total_shipments or via AVG over a CASE expression.
  5. Order by defect_rate DESC, then total_shipments DESC, and limit to the top 3 carriers.