Design a driver-order matching system with VIP, rating and distance rules

Quick Overview

Implement a class that registers drivers and orders and assigns each order to the nearest available driver, with a rating threshold for VIP orders and tie-breaks on rating and driver ID. It tests low-level design, state management and exact, deterministic comparison logic.

Design a driver-order matching system with VIP, rating and distance rules

Company: ByteDance

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a Driver-Order Matching System used by logistics and supply chain teams to assign available drivers to incoming orders based on VIP status, driver ratings, and proximity (Euclidean distance). The system must support registering drivers, creating orders, updating driver status, and matching the best driver to a given order according to a strict set of rules. Matching rules for an order at location `(ox, oy)`: - Only drivers with status `AVAILABLE` are eligible. - If the order is VIP, only drivers with `rating >= 4.5` are eligible. If none qualify, return `-1`. - Among eligible drivers, choose the one with the smallest Euclidean distance to `(ox, oy)`. - Tie-breakers in order: higher rating, then smaller `driverId`. - After a successful match, mark the driver as `BUSY` and the order as `ASSIGNED`. Implement the following methods: - `registerDriver(driverId: int, x: int, y: int, rating: float)`: Register a new driver with the given location and rating. The driver starts with status `AVAILABLE`. - `createOrder(orderId: int, x: int, y: int, isVip: bool)`: Create a new order at the given pickup location with the given VIP flag. The order starts unassigned. - `updateDriverStatus(driverId: int, available: bool)`: Set a driver's status to `AVAILABLE` if `available` is `True`, otherwise `BUSY`. - `matchOrder(orderId: int) → int`: Find the best driver for the given order following the matching rules. Returns the assigned `driverId`, or `-1` if no eligible driver exists. Marks the driver `BUSY` and the order `ASSIGNED` on success. - `isOrderAssigned(orderId: int) → bool`: Return `True` if the order has been successfully matched to a driver, otherwise `False`. This sequence shows the VIP filter and a driver turning `BUSY` after a match: ```text registerDriver(1, 0, 0, 4.8) registerDriver(2, 5, 5, 4.6) registerDriver(3, 1, 1, 4.2) createOrder(10, 1, 1, True) matchOrder(10) -> 1 createOrder(11, 1, 1, False) matchOrder(11) -> 3 isOrderAssigned(11) -> True ``` Driver 3 sits on the pickup point of order 10, but its 4.2 rating is below the VIP threshold, so driver 1 (squared distance 2) beats driver 2 (squared distance 32). Order 11 is not VIP and driver 1 is now `BUSY`, so driver 3 at distance 0 wins. This sequence shows both tie-breakers and a status update: ```text registerDriver(7, 3, 0, 4.5) registerDriver(4, 0, 3, 4.5) registerDriver(9, -3, 0, 4.9) createOrder(20, 0, 0, False) matchOrder(20) -> 9 createOrder(21, 0, 0, True) matchOrder(21) -> 4 updateDriverStatus(4, True) createOrder(22, 0, 0, True) matchOrder(22) -> 4 createOrder(23, 0, 0, True) matchOrder(23) -> 7 createOrder(24, 0, 0, False) matchOrder(24) -> -1 isOrderAssigned(24) -> False ``` All three drivers are at distance 3 from the origin. Order 20 goes to driver 9 on rating. For order 21, drivers 7 and 4 tie on distance and rating, so the smaller ID wins. Driver 4 is set back to `AVAILABLE` and wins order 22 the same way, driver 7 (rated exactly 4.5) takes order 23, and order 24 finds every driver `BUSY`. ```hint One comparison for three rules Look for a single value per driver that you can minimize so that distance, rating and ID are compared in the right order without nested branches. ``` ```hint Exact distance ties Distances are only compared, never returned. Which integer quantity orders drivers the same way as Euclidean distance? ``` ### Constraints & Assumptions - `1 <= driverId, orderId <= 10⁵` - `0.0 <= rating <= 5.0` - `-10⁴ <= x, y <= 10⁴` for all driver and order coordinates - A driver's status is one of `AVAILABLE` or `BUSY` - An order's `isVip` flag is a boolean - At most `10⁴` total operations are performed ### Clarifying Questions to Ask - What should `matchOrder` do when it is called on an order that is already assigned, or on an order ID that was never created? - What happens if `registerDriver` is called with an ID that already exists, and can a driver's location or rating change later? - If an assigned driver is set back to `AVAILABLE`, does the order they were matched to stay assigned? - How many drivers are there compared with match calls? Is a full scan per match acceptable, or do matches need to run faster than linear time? ### What a Strong Answer Covers - The state kept per driver and per order, and the lookups that make registration, order creation, status updates and assignment checks constant time. - A comparison that applies distance, then rating, then driver ID in that order and always picks exactly one driver. - How distances are compared exactly, and why squared distance gives the same ordering as Euclidean distance. - Every outcome of a match: the state changes on success, and the unchanged state when no driver is eligible, when a VIP order has no qualified driver, and when the order is unknown or already assigned. - The cost of each operation, and the point where a linear scan over drivers stops being acceptable. ### Follow-up Questions - With a million drivers and frequent matches, how would you find the nearest eligible driver without scanning all of them, while keeping the same tie-breaking? - Two dispatchers call `matchOrder` at the same moment for different orders. How do you make sure one driver is never assigned to both? - How would you add order cancellation so the driver returns to `AVAILABLE` and the order can be matched again? - How does the design change if drivers report new locations every few seconds?

Overview: Implement a class that registers drivers and orders and assigns each order to the nearest available driver, with a rating threshold for VIP orders and tie-breaks on rating and driver ID. It tests low-level design, state management and exact, deterministic comparison logic.

ByteDance logo
ByteDance
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a Driver-Order Matching System used by logistics and supply chain teams to assign available drivers to incoming orders based on VIP status, driver ratings, and proximity (Euclidean distance).

The system must support registering drivers, creating orders, updating driver status, and matching the best driver to a given order according to a strict set of rules.

Matching rules for an order at location (ox, oy):

  • Only drivers with status AVAILABLE are eligible.
  • If the order is VIP, only drivers with rating >= 4.5 are eligible. If none qualify, return -1 .
  • Among eligible drivers, choose the one with the smallest Euclidean distance to (ox, oy) .
  • Tie-breakers in order: higher rating, then smaller driverId .
  • After a successful match, mark the driver as BUSY and the order as ASSIGNED .

Implement the following methods:

  • registerDriver(driverId: int, x: int, y: int, rating: float) : Register a new driver with the given location and rating. The driver starts with status AVAILABLE .
  • createOrder(orderId: int, x: int, y: int, isVip: bool) : Create a new order at the given pickup location with the given VIP flag. The order starts unassigned.
  • updateDriverStatus(driverId: int, available: bool) : Set a driver's status to AVAILABLE if available is True , otherwise BUSY .
  • matchOrder(orderId: int) → int : Find the best driver for the given order following the matching rules. Returns the assigned driverId , or -1 if no eligible driver exists. Marks the driver BUSY and the order ASSIGNED on success.
  • isOrderAssigned(orderId: int) → bool : Return True if the order has been successfully matched to a driver, otherwise False .

This sequence shows the VIP filter and a driver turning BUSY after a match:

registerDriver(1, 0, 0, 4.8)
registerDriver(2, 5, 5, 4.6)
registerDriver(3, 1, 1, 4.2)
createOrder(10, 1, 1, True)
matchOrder(10)        -> 1
createOrder(11, 1, 1, False)
matchOrder(11)        -> 3
isOrderAssigned(11)   -> True

Driver 3 sits on the pickup point of order 10, but its 4.2 rating is below the VIP threshold, so driver 1 (squared distance 2) beats driver 2 (squared distance 32). Order 11 is not VIP and driver 1 is now BUSY, so driver 3 at distance 0 wins.

This sequence shows both tie-breakers and a status update:

registerDriver(7, 3, 0, 4.5)
registerDriver(4, 0, 3, 4.5)
registerDriver(9, -3, 0, 4.9)
createOrder(20, 0, 0, False)
matchOrder(20)        -> 9
createOrder(21, 0, 0, True)
matchOrder(21)        -> 4
updateDriverStatus(4, True)
createOrder(22, 0, 0, True)
matchOrder(22)        -> 4
createOrder(23, 0, 0, True)
matchOrder(23)        -> 7
createOrder(24, 0, 0, False)
matchOrder(24)        -> -1
isOrderAssigned(24)   -> False

All three drivers are at distance 3 from the origin. Order 20 goes to driver 9 on rating. For order 21, drivers 7 and 4 tie on distance and rating, so the smaller ID wins. Driver 4 is set back to AVAILABLE and wins order 22 the same way, driver 7 (rated exactly 4.5) takes order 23, and order 24 finds every driver BUSY.

Constraints & Assumptions

  • 1 <= driverId, orderId <= 10⁵
  • 0.0 <= rating <= 5.0
  • -10⁴ <= x, y <= 10⁴ for all driver and order coordinates
  • A driver's status is one of AVAILABLE or BUSY
  • An order's isVip flag is a boolean
  • At most 10⁴ total operations are performed

Clarifying Questions to Ask Guidance

  • What should matchOrder do when it is called on an order that is already assigned, or on an order ID that was never created?
  • What happens if registerDriver is called with an ID that already exists, and can a driver's location or rating change later?
  • If an assigned driver is set back to AVAILABLE , does the order they were matched to stay assigned?
  • How many drivers are there compared with match calls? Is a full scan per match acceptable, or do matches need to run faster than linear time?

What a Strong Answer Covers Guidance

  • The state kept per driver and per order, and the lookups that make registration, order creation, status updates and assignment checks constant time.
  • A comparison that applies distance, then rating, then driver ID in that order and always picks exactly one driver.
  • How distances are compared exactly, and why squared distance gives the same ordering as Euclidean distance.
  • Every outcome of a match: the state changes on success, and the unchanged state when no driver is eligible, when a VIP order has no qualified driver, and when the order is unknown or already assigned.
  • The cost of each operation, and the point where a linear scan over drivers stops being acceptable.

Follow-up Questions Guidance

  • With a million drivers and frequent matches, how would you find the nearest eligible driver without scanning all of them, while keeping the same tie-breaking?
  • Two dispatchers call matchOrder at the same moment for different orders. How do you make sure one driver is never assigned to both?
  • How would you add order cancellation so the driver returns to AVAILABLE and the order can be matched again?
  • How does the design change if drivers report new locations every few seconds?