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