Wayfair Machine Coding Practice 4

📌 LeetCode Post – Wayfair Machine Coding Interview: Shipment & Package Management System

🚀 Wayfair Machine Coding Interview (L3) – Shipment & Package Management System

I recently prepared for a Wayfair L3 machine coding round, and one of the frequently asked questions is the Shipment & Package Management System. Here’s the problem statement, my approach, and an optimized Java solution. Hope this helps! 🚀


📌 Problem Statement

Design a Shipment & Package Management System with the following features:

1️⃣ Add a package to the system → addPackage(packageId, weight, distance).
2️⃣ Assign packages to a shipmentassignShipment(shipmentId).
3️⃣ Calculate total shipment costcalculateShipmentCost(shipmentId).
4️⃣ Track shipment statustrackShipment(shipmentId).
5️⃣ Update shipment statusupdateShipmentStatus(shipmentId, status).

🔹 Constraints:

✅ Each shipment has a weight limit of 100 kg.
✅ Packages should be prioritized by weight (heaviest first).
Shipment cost formula:
[
\text{Cost} = \text{$10 (Base Fee)} + (2 \times \text{Total Weight}) + (5 \times \frac{\text{Total Distance}}{100})
]


📌 Approach & Key Concepts

  • PriorityQueue (Max Heap) → Ensures heaviest packages are assigned first (O(N log N)).
  • HashMap for Shipments & Status → Allows quick lookups for shipments and tracking.
  • Basic OOP Design → Single class implementation for simplicity in an interview setting.

📌 Optimized Java Solution

/*📌 3️⃣ Shipment & Package Management System
Problem Statement:
Design a Shipment & Package Management System with the following features:
1.  Add a package to a shipment (addPackage(packageId, weight, destination)).
2.  Assign packages to shipments (assignShipment(shipmentId)).
3.  Calculate total shipment cost based on package weight & distance.
4.  Track shipment status (Pending, In Transit, Delivered).
🔹 Constraints:
✅ Each shipment has a weight limit of 100 kg.
✅ Packages should be prioritized based on weight (heavier first).
✅ Cost = Base Fee ($10) + $2 per kg + $5 per 100 miles.
*/
import java.util.*;
class ShipmentSystem
{
  PriorityQueue<int[]> pq=new PriorityQueue<>((a,b)->b[1]-a[1]);
  HashMap<Integer,List<int[]>> shipments=new HashMap<>();
  HashMap<Integer,String> shipmentStatus=new HashMap<>();

  public void addPackage(int packageId, int weight, int distance)
  {
    pq.add(new int[]{packageId, weight, distance});
  }

  public void assignShipment(int shipmentId)
  {
    if (shipments.containsKey(shipmentId)) {
      throw new IllegalArgumentException("Shipment ID already exists!");
    }
    int weight=0;
    List<int[]> tmp=new ArrayList<>();
    while(!pq.isEmpty() && weight+pq.peek()[1]<=100)
    {
      int[] pkg=pq.remove();
      tmp.add(pkg);
      weight=weight+pkg[1];
    }
    shipments.put(shipmentId, tmp);
    shipmentStatus.put(shipmentId, "Pending");
  }

  public void calculateShipmentCost(int shipmentId)     //based on pkg weight,distance
  {
    if(!shipments.containsKey(shipmentId))
      throw new IllegalArgumentException("Invalid shipment id!");
    List<int[]> shipmentList=shipments.get(shipmentId);
    double totalCost=0;
    for(int[] pkg:shipmentList)
    {
      double cost=pkg[1]*2+(5*pkg[2]/100);
      totalCost+=cost;
    }   
    totalCost+=10;
    System.out.println("Cost of shipment id: "+shipmentId+" = "+totalCost);
  }
  
  public void trackShipment(int shipmentId)    //(Pending, In Transit, Delivered)
  {
    if(!shipmentStatus.containsKey(shipmentId))
      throw new IllegalArgumentException("Invalid shipment id!");
    System.out.println(shipmentStatus.get(shipmentId));
  }

  public void updateShipmentStatus(int shipmentId, String status) {
      if (!shipmentStatus.containsKey(shipmentId))
          throw new IllegalArgumentException("Invalid shipment id!");
      
      shipmentStatus.put(shipmentId, status);
  }
}


class Solution
{
  public static void main(String[] args)
  {
    ShipmentSystem obj=new ShipmentSystem();
    obj.addPackage(1, 30, 500);
    obj.addPackage(2, 50, 1200);
    obj.addPackage(3, 20, 800);
    obj.assignShipment(101);
    obj.trackShipment(101);
    obj.updateShipmentStatus(101, "In Transit");
    obj.trackShipment(101);
    obj.calculateShipmentCost(101);
    obj.updateShipmentStatus(101, "Delivered");
    obj.trackShipment(101);
  }

}

📌 Time Complexity Analysis

OperationTime ComplexityExplanation
Adding a package (addPackage())O(log N)Insert into PriorityQueue (Max Heap).
Assigning a shipment (assignShipment())O(N log N)Extract from heap (N log N sorting).
Tracking a shipment (trackShipment())O(1)Direct HashMap lookup.
Calculating cost (calculateShipmentCost())O(N)Iterating over all packages in shipment.

Most expensive operation is assignShipment(), but it's already optimal at O(N log N).
No unnecessary computations or redundant data structures.


📌 Follow-Up Questions & Possible Optimizations

If you finish coding early, the interviewer may ask follow-up questions:

1️⃣ How can we scale this for millions of shipments & packages?

✅ Use database storage instead of in-memory HashMap.
✅ Use Redis caching for frequently accessed shipment details.
✅ Implement batch processing instead of real-time calculations.

2️⃣ What if packages have different priority rules (e.g., express shipments)?

✅ Modify the PriorityQueue comparator to prioritize based on shipment type.

3️⃣ Can we track individual package delivery status?

✅ Add a packageStatus HashMap to store "Pending", "In Transit", "Delivered" per package.


📌 Similar machine coding interview questions:
🔹 Wayfair L3 Interview – Bangalore (LeetCode)
🔹 Wayfair Machine Coding Round Prep – Q4 2024


Comments (0)