Design a cargo shipping cost calculator for standard, hazardous and fragile packages

Quick Overview

Design a shipping cost calculator that prices standard, hazardous and fragile packages from weight, volume and distance, and reports transport, service and overall cost for each shipment. It tests object-oriented modeling of per-type pricing rules, how easily new types can be added, and careful rounding of money values.

Design a cargo shipping cost calculator for standard, hazardous and fragile packages

Company: Wayfair

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design a cargo shipping cost calculator that prices shipments in three package categories: standard, hazardous and fragile. Each package has a `weight` in kilograms, a `volume` in cubic meters and a `type`. A shipment combines a package with a `distance` in kilometers and has three figures: the transport cost (driven by weight and distance), the service cost (driven by volume and a per-type service rate), and the overall cost (transport plus service, with a per-type surcharge multiplier). The pricing rules per package type are: | Type | Transport cost | Service cost | Surcharge multiplier | |---|---|---|---| | `STANDARD` | `weight * distance * 0.5` | `volume * 10` | `1.0` | | `HAZARDOUS` | `weight * distance * 1.2` | `volume * 25` | `1.5` | | `FRAGILE` | `weight * distance * 0.8` | `volume * 20` | `1.25` | The multiplier applies to transport plus service: overall cost is `(transport + service) * multiplier`. Implement a `ShippingCalculator` that registers shipments by an integer `id` and exposes methods to query each cost component. All returned costs are rounded to 2 decimal places. Rounding applies only to the value a method returns, so the overall cost is computed from the unrounded transport and service costs. Implement the following methods: - `addShipment(id: int, packageType: str, weight: float, volume: float, distance: float) -> None` registers a shipment with the given id. `packageType` is one of `"STANDARD"`, `"HAZARDOUS"` or `"FRAGILE"`. - `getTransportCost(id: int) -> float` returns the transport cost for the shipment. - `getServiceCost(id: int) -> float` returns the service cost for the shipment. - `getOverallCost(id: int) -> float` returns the overall cost: transport plus service, with the per-type surcharge multiplier applied. Example 1: ```text Input: ["ShippingCalculator", "addShipment", "getTransportCost", "getServiceCost", "getOverallCost", "addShipment", "getOverallCost"] [[], [10, "FRAGILE", 5, 1.5, 200], [10], [10], [10], [11, "STANDARD", 1, 1, 1], [11]] Output: [null, null, 800, 30, 1037.5, null, 10.5] ``` The fragile shipment has transport `5 * 200 * 0.8 = 800`, service `1.5 * 20 = 30` and overall `(800 + 30) * 1.25 = 1037.5`. The minimal standard shipment costs `(0.5 + 10) * 1.0 = 10.5` overall. Example 2: ```text Input: ["ShippingCalculator", "addShipment", "getTransportCost", "getServiceCost", "getOverallCost"] [[], [7, "HAZARDOUS", 2, 0.4, 150], [7], [7], [7]] Output: [null, null, 360, 10, 555] ``` Transport is `2 * 150 * 1.2 = 360` and service is `0.4 * 25 = 10`. The hazardous multiplier applies to their sum, so the overall cost is `(360 + 10) * 1.5 = 555`. ```hint Where the differences live Look at what actually changes between the three package types. Put each type's pricing in one place, so the calculator does not need to check the type name in every method. ``` ### Constraints & Assumptions - `1 <= weight <= 10^4` (kilograms) - `0 < volume <= 10^3` (cubic meters) - `1 <= distance <= 10^5` (kilometers) - `packageType` is one of `"STANDARD"`, `"HAZARDOUS"`, `"FRAGILE"` - `1 <= id <= 10^6`, and ids passed to `addShipment` are unique - Every cost query is for an id that was already added ### Clarifying Questions to Ask - When a cost falls exactly on half a cent, which way should it round? - Should an unknown package type or a repeated id raise an error, or can I assume the input is always valid? - How often will new package types or rate changes come in, and should rates be configurable without a code change? - Once a shipment is registered, can its weight, volume or distance change later? ### What a Strong Answer Covers - Where each package type's rates and formulas live, and how the calculator picks them without repeating type checks in every method. - What code changes when a new package type is added. - How money values are represented and rounded, including floating-point error near half a cent. - How shipments are stored and looked up by id, and the cost of each operation. - How invalid input, such as an unknown package type, is handled. ### Follow-up Questions - Hazardous shipments now also pay a flat handling fee per shipment. Where does that change go? - How would you load rates from configuration so pricing can change without a deploy, while shipments already quoted keep their price? - A shipment can now hold several packages of different types. How does the model change? - How would you support distance tiers, such as a lower transport rate for kilometers beyond some threshold?

Overview: Design a shipping cost calculator that prices standard, hazardous and fragile packages from weight, volume and distance, and reports transport, service and overall cost for each shipment. It tests object-oriented modeling of per-type pricing rules, how easily new types can be added, and careful rounding of money values.

Wayfair logo
Wayfair
Nov 24, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a cargo shipping cost calculator that prices shipments in three package categories: standard, hazardous and fragile.

Each package has a weight in kilograms, a volume in cubic meters and a type. A shipment combines a package with a distance in kilometers and has three figures: the transport cost (driven by weight and distance), the service cost (driven by volume and a per-type service rate), and the overall cost (transport plus service, with a per-type surcharge multiplier).

The pricing rules per package type are:

TypeTransport costService costSurcharge multiplier
STANDARDweight * distance * 0.5volume * 101.0
HAZARDOUSweight * distance * 1.2volume * 251.5
FRAGILEweight * distance * 0.8volume * 201.25

The multiplier applies to transport plus service: overall cost is (transport + service) * multiplier.

Implement a ShippingCalculator that registers shipments by an integer id and exposes methods to query each cost component. All returned costs are rounded to 2 decimal places. Rounding applies only to the value a method returns, so the overall cost is computed from the unrounded transport and service costs.

Implement the following methods:

  • addShipment(id: int, packageType: str, weight: float, volume: float, distance: float) -> None registers a shipment with the given id. packageType is one of "STANDARD" , "HAZARDOUS" or "FRAGILE" .
  • getTransportCost(id: int) -> float returns the transport cost for the shipment.
  • getServiceCost(id: int) -> float returns the service cost for the shipment.
  • getOverallCost(id: int) -> float returns the overall cost: transport plus service, with the per-type surcharge multiplier applied.

Example 1:

Input:
["ShippingCalculator", "addShipment", "getTransportCost", "getServiceCost", "getOverallCost", "addShipment", "getOverallCost"]
[[], [10, "FRAGILE", 5, 1.5, 200], [10], [10], [10], [11, "STANDARD", 1, 1, 1], [11]]

Output: [null, null, 800, 30, 1037.5, null, 10.5]

The fragile shipment has transport 5 * 200 * 0.8 = 800, service 1.5 * 20 = 30 and overall (800 + 30) * 1.25 = 1037.5. The minimal standard shipment costs (0.5 + 10) * 1.0 = 10.5 overall.

Example 2:

Input:
["ShippingCalculator", "addShipment", "getTransportCost", "getServiceCost", "getOverallCost"]
[[], [7, "HAZARDOUS", 2, 0.4, 150], [7], [7], [7]]

Output: [null, null, 360, 10, 555]

Transport is 2 * 150 * 1.2 = 360 and service is 0.4 * 25 = 10. The hazardous multiplier applies to their sum, so the overall cost is (360 + 10) * 1.5 = 555.

Constraints & Assumptions

  • 1 <= weight <= 10^4 (kilograms)
  • 0 < volume <= 10^3 (cubic meters)
  • 1 <= distance <= 10^5 (kilometers)
  • packageType is one of "STANDARD" , "HAZARDOUS" , "FRAGILE"
  • 1 <= id <= 10^6 , and ids passed to addShipment are unique
  • Every cost query is for an id that was already added

Clarifying Questions to Ask Guidance

  • When a cost falls exactly on half a cent, which way should it round?
  • Should an unknown package type or a repeated id raise an error, or can I assume the input is always valid?
  • How often will new package types or rate changes come in, and should rates be configurable without a code change?
  • Once a shipment is registered, can its weight, volume or distance change later?

What a Strong Answer Covers Guidance

  • Where each package type's rates and formulas live, and how the calculator picks them without repeating type checks in every method.
  • What code changes when a new package type is added.
  • How money values are represented and rounded, including floating-point error near half a cent.
  • How shipments are stored and looked up by id, and the cost of each operation.
  • How invalid input, such as an unknown package type, is handled.

Follow-up Questions Guidance

  • Hazardous shipments now also pay a flat handling fee per shipment. Where does that change go?
  • How would you load rates from configuration so pricing can change without a deploy, while shipments already quoted keep their price?
  • A shipment can now hold several packages of different types. How does the model change?
  • How would you support distance tiers, such as a lower transport rate for kilometers beyond some threshold?