KatInterview

Auditing a Parcel-Routing Scheduler: Bugs, Structures, and Concurrency

System DesignModel answer
HardModel answerSystem Designcode-reviewconcurrencydata-structures

Last updated

Review a Python parcel-routing codebase for correctness bugs, complexity bottlenecks, and thread-safety failures, then propose concrete fixes for each.

Background

You have just written process_order(...) inside a provided Python skeleton for a parcel-routing system. The codebase has four main classes — Order, Vehicle, Parcel, and Dispatcher — along with a collection of helper functions.

The system's rules:

  • Each vehicle has a fixed departure time and a fixed weight capacity.
  • Each order has an arrival time and one or more parcels, each with a weight.
  • A parcel may only be loaded onto a vehicle that departs strictly after the order arrives.
  • Parcels may be split across multiple vehicles.
  • An order is either fully accepted (all parcels allocated, capacities updated) or fully rejected (the system state is untouched).
  • Allocation follows a deterministic policy: fill vehicles in ascending departure order, breaking ties by vehicle ID.

The interviewer now asks you to step back and review the whole codebase — not just your own function.


What Is Expected

Work through three parts. For each, explain your reasoning and back claims with specific examples or complexity figures.

Part 1 — Correctness

What categories of bugs and unhandled edge cases would you look for in this kind of dispatcher? How would you verify or surface them under time pressure?

Part 2 — Performance

The baseline keeps all vehicles in an unsorted list. Each process_order call linearly scans all n vehicles to find eligible ones, sorts that eligible subset, and fills them one by one. Fleet size is on the order of 10⁵ vehicles; order volume may reach millions per day with bursts of many orders per second.

Analyze the baseline complexity for m orders over n vehicles, identify the bottlenecks, and propose data-structure replacements. State the resulting complexity after your changes, with an amortized argument where appropriate.

Part 3 — Concurrency and Scale

The baseline runs in a single process with no synchronization. Describe precisely what goes wrong when multiple threads call process_order concurrently. Then design an approach that makes concurrent order processing correct. Finally, discuss what you would change if order volume grew beyond what one machine handles comfortably.


Constraints

  • In-memory, single-process Python; no external database or queue in the baseline.
  • Cargo is divisible: an order is accepted if the total remaining capacity of eligible vehicles is at least the total parcel weight. (Bin-packing semantics are explicitly out of scope.)
  • 64-bit integers for all weights and capacities.
  • Rejected orders must produce zero state change.
  • The fleet is static for the lifetime of the process (no arrivals or removals of vehicles).
  • Order cancellations are not required.

Clarifying Questions Worth Raising

Before diving in, a strong candidate would confirm:

  • Is "departs after order time" strict (>) or non-strict (>=)?
  • Are departure times unique, or can multiple vehicles share a timestamp?
  • What consistency guarantee is needed under concurrency — serializable, or weaker?
  • Is there a read-heavy query pattern (e.g., "total remaining capacity on a route") that competes with writes?

Hints

Hint 1

For Part 1, think about what must be true before the first mutation and what invariants must hold after every successful or failed call. Write those down as assertions and drive test cases from them — especially the boundary between a fit and a miss-by-one.

Hint 2

For Part 2, the key observation is that departure times never change once loaded. That lets you sort the fleet once and turn every eligible-set query into a suffix lookup. Think about which data structure answers 'sum of remaining capacity from index i to the end' in O(log n) with point updates, and how you handle already-full vehicles cheaply.

Hint 3

For Part 3, map out the read-check-write sequence inside process_order and ask where two threads can interleave to corrupt state. A global serializing queue is the simplest correct design; explain why per-vehicle locks are tempting but hard to make correct given the deterministic fill order.