PythonStudio
Interview Problems

Food Ordering

medium · order lifecycle, nearest-partner matching

Brief-reported interview attribution (unverified): Amazon, Swiggy, Zomato

How to approach an LLD round

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 otherwise

Rules. 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.

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'])

Starter code

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
Loading editor…

Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.

Return to the Python compiler