PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates the ability to implement robust input and business-rule validation for shopping-cart items, including data integrity checks, inventory constraints, and structured error reporting.

  • medium
  • DoorDash
  • Coding & Algorithms
  • Software Engineer

Validate a Shopping Cart

Company: DoorDash

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are building the cart validation logic for a food-delivery application. Implement a function `validateCart(cart, catalog)` that checks whether a customer's cart is valid before checkout. Each cart contains a list of items. Each cart item has: - `itemId`: unique identifier of the menu item - `quantity`: number of units requested The catalog/inventory data for each item contains: - `itemId` - `availableQuantity`: how many units are currently available - `minQuantity`: minimum allowed quantity for that item in one cart - `maxQuantity`: maximum allowed quantity for that item in one cart Your function should validate at least the following: 1. Every cart item exists in the catalog. 2. The requested quantity is a positive integer. 3. The requested quantity does not exceed the item's available quantity. 4. The requested quantity is greater than or equal to `minQuantity`. 5. The requested quantity is less than or equal to `maxQuantity`. Return a structured validation result, for example: - `isValid: true` and an empty error list if the cart is valid. - `isValid: false` and a list of validation errors if the cart is invalid. Discuss edge cases with the interviewer, such as duplicate `itemId`s in the cart, empty carts, missing catalog fields, and whether validation should fail fast or collect all errors.

Quick Answer: This question evaluates the ability to implement robust input and business-rule validation for shopping-cart items, including data integrity checks, inventory constraints, and structured error reporting.

You are building cart validation logic for a food-delivery application. Implement `solution(cart, catalog)` to validate a customer's cart before checkout. Each cart item is represented as a dictionary with: - `itemId`: the menu item identifier - `quantity`: the number of units requested Each catalog item is represented as a dictionary with: - `itemId` - `availableQuantity` - `minQuantity` - `maxQuantity` The function must collect all validation errors, not fail fast. Validation rules: 1. The cart must not be empty. 2. Every cart item must have an `itemId`. 3. Every cart item must have a `quantity`. 4. Each cart line quantity must be a positive integer. 5. Every cart item must exist in the catalog. 6. Duplicate `itemId`s in the cart are allowed, but their quantities are combined before checking availability, minimum quantity, and maximum quantity. 7. For every referenced catalog item, required catalog fields must exist and be integers. 8. The total requested quantity for an item must not exceed `availableQuantity`. 9. The total requested quantity for an item must be at least `minQuantity`. 10. The total requested quantity for an item must be at most `maxQuantity`. Return a dictionary with: - `isValid`: `True` if there are no errors, otherwise `False` - `errors`: a list of error dictionaries Errors should be returned in deterministic order: cart-line errors in cart order first, then aggregate item errors in the order each valid item first appears in the cart.

Constraints

  • 0 <= len(cart) <= 100000
  • 0 <= len(catalog) <= 100000
  • Catalog `itemId`s are unique.
  • When present, `itemId` values are non-null and hashable.
  • A valid quantity is an integer greater than 0; boolean values are not considered valid integers.
  • Duplicate cart item IDs must be aggregated before checking item-level limits.
  • The function must collect all validation errors rather than stopping at the first error.

Examples

Input: ([{'itemId': 'burger', 'quantity': 2}, {'itemId': 'fries', 'quantity': 1}], [{'itemId': 'burger', 'availableQuantity': 5, 'minQuantity': 1, 'maxQuantity': 3}, {'itemId': 'fries', 'availableQuantity': 10, 'minQuantity': 1, 'maxQuantity': 5}])

Expected Output: {'isValid': True, 'errors': []}

Explanation: Both items exist, both quantities are positive integers, and both requested quantities are within availability and min/max limits.

Input: ([{'itemId': 'burger', 'quantity': 4}, {'itemId': 'soda', 'quantity': 0}, {'itemId': 'pizza', 'quantity': 1}], [{'itemId': 'burger', 'availableQuantity': 3, 'minQuantity': 1, 'maxQuantity': 5}, {'itemId': 'soda', 'availableQuantity': 10, 'minQuantity': 1, 'maxQuantity': 6}])

Expected Output: {'isValid': False, 'errors': [{'index': 1, 'code': 'INVALID_QUANTITY'}, {'index': 2, 'itemId': 'pizza', 'code': 'ITEM_NOT_FOUND'}, {'itemId': 'burger', 'code': 'EXCEEDS_AVAILABLE'}]}

Explanation: The soda quantity is not positive, pizza does not exist in the catalog, and the requested burger quantity exceeds the available quantity of 3.

Input: ([{'itemId': 'taco', 'quantity': 2}, {'itemId': 'taco', 'quantity': 3}, {'itemId': 'water', 'quantity': 1}], [{'itemId': 'taco', 'availableQuantity': 10, 'minQuantity': 1, 'maxQuantity': 4}, {'itemId': 'water', 'availableQuantity': 5, 'minQuantity': 1, 'maxQuantity': 2}])

Expected Output: {'isValid': False, 'errors': [{'itemId': 'taco', 'code': 'ABOVE_MAX'}]}

Explanation: Duplicate taco entries are combined for a total quantity of 5, which exceeds the per-cart maximum of 4.

Input: ([], [{'itemId': 'burger', 'availableQuantity': 5, 'minQuantity': 1, 'maxQuantity': 3}])

Expected Output: {'isValid': False, 'errors': [{'code': 'EMPTY_CART'}]}

Explanation: An empty cart cannot be checked out.

Input: ([{'itemId': 'sushi', 'quantity': 1}, {'itemId': 'soup', 'quantity': 2}], [{'itemId': 'sushi', 'availableQuantity': 5, 'minQuantity': 2, 'maxQuantity': 4}, {'itemId': 'soup', 'availableQuantity': 10, 'minQuantity': 1}])

Expected Output: {'isValid': False, 'errors': [{'itemId': 'sushi', 'code': 'BELOW_MIN'}, {'itemId': 'soup', 'code': 'MISSING_CATALOG_FIELD', 'field': 'maxQuantity'}]}

Explanation: The sushi quantity is below its minimum of 2. The soup catalog entry is missing the required `maxQuantity` field.

Input: ([{'quantity': 1}, {'itemId': 'tea'}], [{'itemId': 'tea', 'availableQuantity': 4, 'minQuantity': 1, 'maxQuantity': 2}])

Expected Output: {'isValid': False, 'errors': [{'index': 0, 'code': 'MISSING_ITEM_ID'}, {'index': 1, 'code': 'MISSING_QUANTITY'}]}

Explanation: The first cart line has no itemId, and the second cart line has no quantity.

Hints

  1. Build a hash map from `itemId` to catalog entry so each cart item can be checked in O(1) average time.
  2. Because duplicate cart items are allowed, first validate individual cart lines, then aggregate valid quantities by `itemId` before checking availability and min/max limits.
Last updated: Jun 13, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Timeout Refund Workflow - DoorDash (medium)
  • Calculate Driver Payments - DoorDash (medium)
  • Maximize Chef Assignment Profit - DoorDash (medium)
  • Compute Courier Delivery Pay - DoorDash (easy)
  • Compute Nearest Destination Distances - DoorDash (easy)