Food Ordering
medium · order lifecycle, nearest-partner matching
Brief-reported interview attribution (unverified): Amazon, Swiggy, Zomato
How to approach an LLD roundRequirements
- Functional: register restaurants (menu + location) and delivery partners, place an order (nearest free partner to the restaurant, total = items + $2 fee), walk the order placed → preparing → picked up → delivered, cancel from placed/preparing.
- Non-functional: only the exact next step is a valid transition; a partner must never carry two orders; the status machine must live on the order, not the system.
- Out of scope: payments, restaurant preparation times, surge fees.
Statement
Design a food ordering system. Restaurants have menus (item →
price in cents) and a location. Delivery partners have locations and are busy
while they carry an order. Placing an order assigns the **nearest available
partner to the restaurant** (ties break to the lowest partner id) and charges the
items' total plus a flat $2 (200¢) delivery fee. An order moves
"placed" → "preparing" → "picked up" → "delivered", and can be cancelled from
"placed" or "preparing". On delivery the partner becomes available at the
customer's location; on cancellation the partner becomes available where they are.
class FoodOrdering:
def __init__(self):
def add_restaurant(self, restaurant_id, menu, x, y):
# menu: {item: price_cents}
def add_delivery_partner(self, partner_id, x, y):
def place_order(self, customer_id, restaurant_id, items, customer_x, customer_y):
# -> order id (int, starting at 1), or None if the restaurant is unknown,
# the item list is empty, an item is not on the menu, or no partner is free
def order_status(self, order_id):
# -> "placed" | "preparing" | "picked up" | "delivered" | "cancelled" | "unknown"
def order_total(self, order_id):
# -> total in cents, or None for an unknown order
def order_details(self, order_id):
# -> {"customer":, "restaurant":, "items": [...], "total":, "partner":, "status":}, or None
def update_status(self, order_id, new_status):
# -> True if the transition is valid, False otherwise
# (advance one step at a time; "cancelled" is allowed from "placed"/"preparing")
def cancel_order(self, order_id):
# -> True if the order was cancellable, False otherwiseRules. An order id is assigned only on success. A partner is busy from
assignment until the order is delivered or cancelled. update_status may only
advance one step (placed → preparing → picked up → delivered); it may not skip,
go backwards, or change a delivered/cancelled order. Cancelling frees the partner
at their current location.
Money units: menu prices and totals are integer cents; the delivery fee is 200 cents.
Tests use explicit inputs and injected clocks/callbacks. No network, real threads or application-server execution is involved.
Class design (SOLID)
Restaurant (menu + location; SRP). DeliveryPartner (location + availability; SRP). Order (items, total, partner, and the state machine; SRP). FoodOrdering orchestrates and matches the nearest partner. Pattern: State machine (order lifecycle).
Public demo
# Public demo: run after implementing the class API above.
f = FoodOrdering()
f.add_restaurant('R1', {'Burger': 500, 'Fries': 200}, 0, 0)
f.add_delivery_partner('D1', 1, 0)
f.add_delivery_partner('D2', 10, 0)
print(f.place_order('C1', 'R1', ['Burger', 'Fries'], 3, 4))
print(f.order_total(1))
print(f.order_details(1)['partner'])Reference solution and hidden tests
Locked. Open the interactive editor and attempt the problem; the reference, hidden test descriptions, pattern explanation and complexity are available only after an explicit reveal.
Concurrency
Two orders placed at once must not get the same partner: the find-nearest + mark-busy step must be atomic — one lock around place_order. The state transitions are already single-step, so a stale update_status cannot skip a state. The reference is single-threaded; the lock is described here.
Trade-offs & extensions
Nearest-to-restaurant is simple but can starve a far partner; a load-balanced assignment would need a MatchingStrategy. Extending: a PartnerLocation feed (partners move) and an OrderTracker (Observer) for status updates.
Starter code
import math
class Restaurant:
def __init__(self, restaurant_id, menu, x, y):
"""Initialize the state described in the problem."""
pass
def price(self, item):
"""Implement the return value and rules in the API contract."""
pass
class DeliveryPartner:
def __init__(self, partner_id, x, y):
"""Initialize the state described in the problem."""
pass
class Order:
def __init__(self, order_id, customer_id, restaurant, items, customer_location, partner):
"""Initialize the state described in the problem."""
pass
def advance(self, new_status):
"""Implement the return value and rules in the API contract."""
pass
def details(self):
"""Implement the return value and rules in the API contract."""
pass
class FoodOrdering:
def __init__(self):
"""Initialize the state described in the problem."""
pass
def add_restaurant(self, restaurant_id, menu, x, y):
"""menu: {item: price_cents}"""
pass
def add_delivery_partner(self, partner_id, x, y):
"""Implement the return value and rules in the API contract."""
pass
def place_order(self, customer_id, restaurant_id, items, customer_x, customer_y):
"""-> order id (int, starting at 1), or None if the restaurant is unknown, the item list is empty, an item is not on the menu, or no partner is free"""
pass
def order_status(self, order_id):
'''-> "placed" | "preparing" | "picked up" | "delivered" | "cancelled" | "unknown"'''
pass
def order_total(self, order_id):
"""-> total in cents, or None for an unknown order"""
pass
def order_details(self, order_id):
"""-> {"customer":, "restaurant":, "items": [...], "total":, "partner":, "status":}, or None"""
pass
def update_status(self, order_id, new_status):
"""-> True if the transition is valid, False otherwise (advance one step at a time; "cancelled" is allowed from "placed"/"preparing")"""
pass
def cancel_order(self, order_id):
"""-> True if the order was cancellable, False otherwise"""
pass
Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.
Return to the Python interpreter