KatInterview

Carrier Defect Metrics and High-Risk Shipping Routes

SQL & DataAuto-graded · 14 test cases
MediumAuto-gradedSQL & Dataaggregationjoinswindow-functions

Last updated

Aggregating shipment quality metrics by carrier and time period, computing period-over-period changes, and ranking routes by defect frequency.

You are analyzing logistics data to track shipment quality across carriers and identify problematic shipping routes.

You are given two data structures:

shipments: A list of dictionaries, each representing a shipment with fields:

  • shipment_id (int): unique identifier
  • order_id (int): associated order
  • ship_date (str): date the shipment departed, in ISO format (YYYY-MM-DD)
  • carrier (str): shipping company (e.g., "UPS", "FedEx", "USPS")
  • origin (str): departure location code
  • destination (str): arrival location code
  • status (str): current status

defects: A list of dictionaries, each representing a quality issue with fields:

  • defect_id (int): unique identifier
  • shipment_id (int): references a shipment
  • defect_type (str): category of issue (e.g., "Damaged", "Lost", "LateDelivery")
  • defect_reported_date (str): date the issue was reported, in ISO format

reference_date (str): the analysis anchor date in ISO format.

Your task is to produce a dictionary with two keys:

  1. "weekly": A list of weekly defect rate records for each carrier, covering shipments that departed in the 30-day window ending on reference_date (inclusive). Each record contains:

    • carrier (str)
    • week_start (str): the Monday starting that ISO week, in ISO format
    • defect_rate (float): total defects divided by total shipments for that carrier in that week
    • wow_pct_change (float or None): percentage change from the previous week's rate for this carrier, or None if there is no previous week or the previous rate was zero

    Sort by carrier (ascending), then by week_start (ascending).

  2. "top_lanes": A list of up to 3 origin-destination pairs with the highest count of "Damaged" defects reported in the 90-day window ending on reference_date (inclusive). Each record contains:

    • origin (str)
    • destination (str)
    • damaged_defect_count (int): number of Damaged defects on this route

    Sort by count (descending), then by origin (ascending), then by destination (ascending). Return at most 3 records.

Important details:

  • The 30-day window for weekly analysis includes all shipments where reference_date - 29 days ≤ ship_date ≤ reference_date.
  • The 90-day window for lane analysis includes all defects where reference_date - 89 days ≤ defect_reported_date ≤ reference_date.
  • A shipment with multiple defects contributes all of them to the defect count (the rate can exceed 1.0).
  • Each shipment is counted exactly once in the denominator for its carrier and week, regardless of how many defects it has.
  • The week starts on Monday. Use ISO week calculation (Monday = day 0).
  • For week-over-week change, compare each week to the immediately preceding week in the sorted list for that carrier, not necessarily the previous calendar week.
  • If the previous week's rate is zero, wow_pct_change should be None to avoid division by zero.
  • Defects linked to shipments not present in the shipments list should be ignored.

Constraints:

  • 0 ≤ len(shipments) ≤ 10,000
  • 0 ≤ len(defects) ≤ 50,000
  • All dates are valid ISO format strings
  • shipment_id values in defects may or may not match shipments
  • Carrier, origin, and destination strings are non-empty and at most 50 characters
  • Defect types are non-empty strings

Examples

Example 1

Input: compute_carrier_metrics([{'shipment_id': 1001, 'order_id': 5001, 'ship_date': '2023-09-04', 'carrier': 'UPS', 'origin': 'SEA', 'destination': 'NYC', 'status': 'Delivered'}, {'shipment_id': 1002, 'order_id': 5002, 'ship_date': '2023-09-05', 'carrier': 'UPS', 'origin': 'SEA', 'destination': 'CHI', 'status': 'Delivered'}, {'shipment_id': 1003, 'order_id': 5003, 'ship_date': '2023-09-11', 'carrier': 'UPS', 'origin': 'LAX', 'destination': 'NYC', 'status': 'Delivered'}, {'shipment_id': 1004, 'order_id': 5004, 'ship_date': '2023-09-12', 'carrier': 'FedEx', 'origin': 'LAX', 'destination': 'CHI', 'status': 'Delivered'}], [{'defect_id': 1, 'shipment_id': 1001, 'defect_type': 'Damaged', 'defect_reported_date': '2023-09-06'}, {'defect_id': 2, 'shipment_id': 1001, 'defect_type': 'LateDelivery', 'defect_reported_date': '2023-09-07'}, {'defect_id': 3, 'shipment_id': 1003, 'defect_type': 'Damaged', 'defect_reported_date': '2023-09-13'}, {'defect_id': 4, 'shipment_id': 1004, 'defect_type': 'Lost', 'defect_reported_date': '2023-09-14'}], '2023-09-30')

Output: {'weekly': [{'carrier': 'FedEx', 'week_start': '2023-09-11', 'defect_rate': 1, 'wow_pct_change': None}, {'carrier': 'UPS', 'week_start': '2023-09-04', 'defect_rate': 1, 'wow_pct_change': None}, {'carrier': 'UPS', 'week_start': '2023-09-11', 'defect_rate': 1, 'wow_pct_change': 0}], 'top_lanes': [{'origin': 'LAX', 'destination': 'NYC', 'damaged_defect_count': 1}, {'origin': 'SEA', 'destination': 'NYC', 'damaged_defect_count': 1}]}

Example 2

Input: compute_carrier_metrics([{'shipment_id': 1, 'order_id': 1, 'ship_date': '2024-01-15', 'carrier': 'USPS', 'origin': 'A', 'destination': 'B', 'status': 'Delivered'}, {'shipment_id': 2, 'order_id': 2, 'ship_date': '2024-01-16', 'carrier': 'FedEx', 'origin': 'A', 'destination': 'C', 'status': 'Delivered'}, {'shipment_id': 3, 'order_id': 3, 'ship_date': '2024-02-05', 'carrier': 'USPS', 'origin': 'A', 'destination': 'B', 'status': 'Delivered'}], [{'defect_id': 1, 'shipment_id': 1, 'defect_type': 'Damaged', 'defect_reported_date': '2024-01-16'}, {'defect_id': 2, 'shipment_id': 3, 'defect_type': 'Damaged', 'defect_reported_date': '2024-02-06'}], '2024-02-10')

Output: {'weekly': [{'carrier': 'FedEx', 'week_start': '2024-01-15', 'defect_rate': 0, 'wow_pct_change': None}, {'carrier': 'USPS', 'week_start': '2024-01-15', 'defect_rate': 1, 'wow_pct_change': None}, {'carrier': 'USPS', 'week_start': '2024-02-05', 'defect_rate': 1, 'wow_pct_change': 0}], 'top_lanes': [{'origin': 'A', 'destination': 'B', 'damaged_defect_count': 2}]}

Hints

Hint 1

Build an index of defects per shipment first to avoid double-counting shipments with multiple defects in your denominator

Hint 2

Use ISO week calculation: the week starts on Monday, so subtract the weekday number from the ship date to find the Monday of that week

Hint 3

When computing week-over-week change, compare consecutive weeks in your sorted data per carrier, not calendar weeks—gaps are allowed