Validate a Shopping Cart
DoorDashSoftware Engineer · Technical ScreenLast updated
Implement a shopping cart validation function that checks item availability, consolidates duplicate entries, verifies inventory levels, and ensures order minimums are satisfied.
Problem Statement
In online food and grocery delivery platforms, shopping carts must be validated against real-time store catalogs before allowing a customer to proceed to checkout.
Implement the function validate_cart(store_info: dict, cart_items: list[dict]) -> dict that validates a user's cart against a store's catalog and ordering rules.
Rules
- Empty Cart: If
cart_itemsis empty, return an error"EMPTY_CART". - Invalid Quantity: Each item in
cart_itemsmust have an integerquantity > 0. If not, flag"INVALID_QUANTITY: ". - Catalog Existence: Each
item_idmust exist instore_info["catalog"]. If not found, flag"ITEM_NOT_FOUND: ". - Inventory Check: Multiple entries with the same
item_idincart_itemsshould be consolidated (summing their quantities). For each existing item with valid quantities, iftotal_quantity > catalog[item_id]["inventory"], flag"INSUFFICIENT_INVENTORY: ". - Minimum Order Value: If there are no catalog or inventory errors, compute the cart's subtotal (
sum(catalog[item_id]["price"] * total_quantity)). Ifsubtotal < store_info["min_order_value"], flag"BELOW_MIN_ORDER".
Errors should be collected into a list. Order errors deterministically: list item-level errors in the order of their first appearance in cart_items, followed by "BELOW_MIN_ORDER" if applicable. An item should not be flagged for INSUFFICIENT_INVENTORY if it already failed the ITEM_NOT_FOUND or INVALID_QUANTITY check.
Function Signature
def validate_cart(store_info: dict, cart_items: list[dict]) -> dict:
pass
Input and Output Format
store_info: A dictionary with:"min_order_value"(float): Minimum monetary value required to place an order."catalog"(dict): Maps stringitem_idto{"price": float, "inventory": int}.
cart_items: A list of dictionaries, each containing"item_id"(str) and"quantity"(int).
Return a dictionary with:
"is_valid"(bool):Trueiferrorsis empty, otherwiseFalse."subtotal"(float): The subtotal rounded to 2 decimal places (return0.0if any item-level errors occurred)."errors"(list[str]): List of error strings.
Example 1
store_info = {
"min_order_value": 15.00,
"catalog": {
"item_1": {"price": 5.50, "inventory": 10},
"item_2": {"price": 12.00, "inventory": 1}
}
}
cart_items = [
{"item_id": "item_1", "quantity": 2},
{"item_id": "item_2", "quantity": 2}
]
# Output:
# {
# "is_valid": False,
# "subtotal": 0.0,
# "errors": ["INSUFFICIENT_INVENTORY: item_2"]
# }
Explanation: item_2 requested quantity is 2, but available inventory is only 1.
Example 2
store_info = {
"min_order_value": 20.00,
"catalog": {
"item_1": {"price": 4.00, "inventory": 10}
}
}
cart_items = [
{"item_id": "item_1", "quantity": 2},
{"item_id": "item_1", "quantity": 1}
]
# Output:
# {
# "is_valid": False,
# "subtotal": 0.0,
# "errors": ["BELOW_MIN_ORDER"]
# }
Explanation: item_1 is consolidated to quantity 3. Total price is 3 * 4.00 = 12.00, which is below min_order_value of 20.00.
Constraints
0 = 0
Hints
Hint 1
First check for the empty cart condition and validate individual entry attributes (such as positive quantities and catalog presence).
Hint 2
Consolidate duplicate cart items by summing their quantities using a hash map before comparing against store inventory.
Hint 3
Only calculate subtotal and verify minimum order value if all item-level validations (presence, quantity, and inventory) pass.
Follow-up questions
- How would you handle item-level modifier selections (e.g., extra cheese, dietary substitutions) that alter item price and have their own inventory?
- If the catalog inventory changes concurrently while this validation is running, how would you ensure data consistency without locking the entire store catalog?
- How would you extend this function to support store-level or item-level promotional discounts (e.g., 'Buy 1, Get 1 Free') during cart validation?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.