Flexport logoFlexport
CodingFree preview

Order-to-Voyage Booking System

Model orders (origin/destination port, dropoff date, target delivery date) and voyages (route, departure, arrival). Part 1: `book_order` assigns to the earliest valid voyage. Part 2: per-voyage capacity. Part 3: book an order if moving a single existing booking frees room. A 2026 variant replaces Part 3 with per-ship overload percentages, preferring non-overloaded ships.

SWE
medium
object-design
scheduling
simulation
sorting
Frequency
Low
Last asked
2026-04-01
Stage
—

Problem

Build a booking system that assigns shipping orders to voyages. The data model is up to you.

  • An order has an origin port, a destination port, a dropoff date (when the client's goods are ready to ship), and a target delivery date (the deadline for delivery).
  • A voyage is a shipping route between an origin port and a destination port, with a specific departure date and arrival date.

Part 1 — Book to the earliest voyage

Write book_order, which assigns the given order to the earliest possible voyage (by departure date) that satisfies the order's needs: same route, departs no earlier than the dropoff date, and arrives by the target delivery date.

Part 2 — Capacity

Each voyage carries at most a fixed number of orders.

Part 3 — Rescheduling one order

Book an order if rescheduling a single other booked order opens capacity.

Examples

Part 3 scenario, as given in the prompt:

Voyage1: SZ (08/09) -> LA (08/20)
  - Order1: Dropoff (08/09), Target Delivery (08/30)
Voyage2: SZ (08/10) -> LA (08/21)
  - {Available capacity}

A new Order2 from SZ to LA with dropoff 08/09 and target delivery 08/20 can't be booked as-is: Voyage1 is full and Voyage2 arrives too late. Moving Order1 to Voyage2 (it still meets its 08/30 deadline) frees Voyage1 for Order2.

Variants

Overload allowance instead of rescheduling (2026)

The same three-step structure appears with a different final step, framed as weighted cargo onto ships:

  1. Book orders ignoring capacity (any feasible assignment).
  2. Enforce capacity: the total weight on a ship must not exceed its capacity.
  3. Each ship has its own overload percentage it may exceed capacity by, so max_load = capacity * (1 + overload_percentage). Prefer ships that can take the order without overloading; only if none exist, fall back to ships that would need to use their overload allowance.
def assign_with_overload(
    cargo_weights: List[int],
    ship_capacities: List[int],
    overload_pcts: List[float],
) -> Optional[List[List[int]]]:
    # ship i max load = ship_capacities[i] * (1 + overload_pcts[i])
    # returns cargo indices per ship, or None if infeasible

Clarify which version of the last step you are getting before coding.

Hints

Pitfalls

  • Each part extends the previous one, so a model that hard-codes Part 1's assumptions makes Parts 2–3 painful. Keep orders, voyages, and the booking assignment as separate, queryable state.
  • In Part 3, the relocated order must still satisfy its own constraints (route, dropoff, deadline) on its new voyage, and only one existing order may be moved.
  • Dates are inclusive at the dropoff end: in the example, an order with dropoff 08/09 can board a voyage departing 08/09.
  • Overload variant: "prefer no overload" means few or no overloaded ships, not just any feasible assignment; the move-light-items post-pass is a heuristic, so say so.
  • Overload variant: check total weight against total max load before any work, and bail out as soon as the best ship cannot hold the current item, treating that as "the greedy failed" rather than proof that no assignment exists.

Talking Points

  • State the greedy choice before coding (heaviest item first, most capacious ship first); the interviewer can correct course early.
  • Say clearly that the greedy can fail on feasible inputs (capacities [10, 10], cargo [6, 5, 4, 3, 2]), and offer backtracking when an exact answer is required.
  • Use a max-heap rather than a sorted list: O(log m) per pop versus O(m).
  • Present overload as two phases: zero overload first, then relax to max load.
  • Admit the post-processing is a heuristic with no minimum-overload guarantee; exact answers would need backtracking or DP.

Follow-ups

Preparation

  • Implement Order, Voyage, and book_order for Parts 1–2 from scratch in under 25 minutes, then add single-move rescheduling and check it against the SZ → LA example.
  • Implement the overload variant: base capacity plus a per-voyage overload percentage, with a first pass over non-overloaded voyages and a fallback pass over overloadable ones.
  • Write a few tests per part (no matching voyage, all voyages full, rescheduling succeeds, rescheduling impossible) so you can show working code at each step.
Was this article helpful?

Comments

Sign in to join the discussion
Loading...