KatInterview

Design a Container Assignment System with Compatibility Constraints

System DesignModel answer
HardModel answerSystem Designsystem-designdistributed-systemsoptimization

Last updated

Design an automated container packing platform that optimizes cargo placement under weight, volume, and hazmat rules, supporting async runs, plan overrides, and strict determinism.

Context

In ocean logistics, freight consolidators combine shipments from different cargo owners into standardized sea containers (20ft, 40ft, High Cube, Refrigerated). Cargo items differ by dimensions, weight limits, stacking rules, temperature requirements, and dangerous goods classifications (e.g., IMDG classes). Specific combinations of goods are strictly prohibited from sharing a container (e.g., volatile organic liquids cannot be stored with strong oxidizers or human foodstuffs).

Design an automated planning service that assigns cargo items to available container types to minimize container procurement costs while adhering to all physical, temperature, and hazmat compatibility rules. The platform must also support asynchronous processing, operator reviews, and manual overrides.


Requirements

Functional Requirements

  1. Optimization Engine: Given a list of cargo items and supported container types, compute assignments that satisfy volume, mass payload, temperature, and material incompatibility constraints while minimizing cost.
  2. Asynchronous Execution: Planning tasks must run asynchronously, allowing clients to track job status via polling or webhooks.
  3. Strict Determinism: Re-running the solver on identical inputs (cargo manifest revision, container specs, rule version, configuration, and random seed) must produce identical output every time.
  4. Actionable Infeasibility Explanations: If a cargo set cannot be fully loaded legally or physically, return the minimal conflicting item subset and the specific rules breached.
  5. Drafting, Overrides, & Manual Approvals: Generated plans are stored as drafts. Human dispatchers can manually move items between containers (validated on-the-fly against constraints) and approve the final plan.
  6. Stale Input & Double-Allocation Prevention: Prevent a plan from being approved if the underlying cargo manifest changes during calculation or review, or if any cargo item has already been assigned in another approved plan.
  7. Audit Trails: Maintain an immutable, tamper-evident log of manifest revisions, solver outputs, manual user modifications, and final plan approvals.

Non-Functional Requirements & Scale

  • Traffic: ~15,000 planning requests per day, with peak concurrency reaching 100 simultaneous optimization executions.
  • Payload Volume: Typical manifest size ranges between 50 and 500 items; large consolidation runs contain up to 5,000 items.
  • Latency Budget: 95th percentile under 30 seconds for typical plans (≤500 items); under 120 seconds for large plans (5,000 items).
  • Consistency: Strong transactional consistency for final plan approvals and inventory allocation locks.

What Your Design Should Cover

  1. API Contracts: REST/gRPC interfaces for job creation, progress tracking, conflict inspection, manual item migration, and plan approval.
  2. Data Model & Schema: Tables for Cargo Items, Incompatibility Rules, Container Catalogs, Draft Plans, Assignments, and Audit Records.
  3. System Architecture: Storage layout, worker queues, job lifecycle, and solver execution isolation.
  4. Concurrency & Race Condition Handling: Strategies (e.g., Optimistic Concurrency Control, input hashes, database locks) to avoid double allocation and stale approvals.
  5. Explainability Strategy: Algorithmic approach to isolate Minimal Unsatisfiable Subsets (MUS) when a plan is infeasible.

Hints

Hint 1

Divide solver workers into separate queues based on manifest size to prevent small jobs from being starved by massive 5,000-item optimization runs.

Hint 2

Achieve solver determinism by bounding execution using deterministic solver time units rather than wall-clock time, and fixing the thread count and random seed.

Hint 3

Use a canonical input hash (covering items, rules, and seeds) combined with database locks to ensure that stale manifest revisions cannot be approved.