Validate a Shopping Cart
Company: DoorDash
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
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.
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
- Build a hash map from `itemId` to catalog entry so each cart item can be checked in O(1) average time.
- Because duplicate cart items are allowed, first validate individual cart lines, then aggregate valid quantities by `itemId` before checking availability and min/max limits.