KatInterview

Real-Time Courier-to-Delivery Matching Engine Design

Greedy & IntervalsModel answer
MediumModel answerGreedy & Intervalsmatchinggreedyspatial-index

Last updated

Design a scalable real-time engine that matches couriers to incoming delivery requests, defining the cost model, candidate pruning, matching method, complexity, and failure handling.

Courier-to-Delivery Matching Engine

A food delivery platform receives a constant stream of new delivery requests. At the same moment, thousands of couriers are moving around, going online and offline, finishing trips, and declining offers. You must design the component that decides which courier is offered which request.

What to cover

  1. Objectives. State what the system optimizes. Consider customer lateness, courier idle time at the pickup location, offer declines and cancellations, and fairness in how work and earnings are distributed. Explain how you would combine them into a single cost per courier/request pair.
  2. Constraints. Define which pairs are infeasible: remaining capacity (including batching of multiple requests), maximum distance, latest acceptable pickup time, and vehicle compatibility.
  3. Signals. List the inputs you would use, such as location and its age, courier status, vehicle type, historical acceptance rate, ratings, food preparation estimates, and demand surge.
  4. Algorithm and data structures. Propose an approach (for example greedy with priority queues, bipartite matching or min-cost flow, or scoring plus auction) that holds up under heavy churn and incomplete information.
  5. Complexity. Give time and space analysis in terms of the number of couriers, requests, and candidate edges.
  6. Robustness. Explain how you handle delayed or stale location updates, reassignment after declines, and adversarial or gaming behavior.

Assumptions you may use

Take a large metro region with roughly 10,000 active couriers and 2,000 pending requests, with decisions refreshed every few seconds.

Expected output

A structured design discussion. A short code sketch of the core matching loop is welcome.

Hints

Hint 1

Do not assign each request instantly to the nearest courier, and do not run a dense optimal solver over the whole city. Think about collecting requests into a short micro-batch window and solving each window.

Hint 2

Shrink the bipartite graph first: index couriers in a spatial grid, look only at nearby cells for each request, and keep the top K feasible candidates, so edges number at most M times K.

Hint 3

Fold everything into one expected-cost edge score (lateness, waiting, fairness, acceptance probability), then take edges in ascending cost order while checking capacity. Add an aging bonus so unmatched requests do not starve, and inflate ETAs for stale locations.