Compute cart total with best promotion
Checkout Price Calculator with Best Promotion
Assume you are implementing a checkout price calculator for an online shopping cart.
Input Data
You will be provided with:
- A list of items in the cart: Each item has a
price, represented as a non-negative decimal number. - A
shipping_fee: A non-negative decimal number representing the shipping cost for the entire order. - A list of promotions: Each promotion is one of two types:
- Percentage discount: Represented by a percentage value
p(e.g.,10for 10%). This discount applies only to the sum of item prices, not to the shipping fee. - Fixed-amount discount: Represented by a non-negative decimal
d. This discount is subtracted from the total amount including shipping.
- Percentage discount: Represented by a percentage value
Rules and Assumptions
- Let
items_sumbe the sum of all item prices. - Before any discount, the total amount is
items_sum + shipping_fee. - For a percentage discount
p(e.g., 10 for 10%):discounted_items = items_sum * (1 - p/100)total = discounted_items + shipping_fee
- For a fixed-amount discount
d:total = max(0, items_sum + shipping_fee - d)
- Percentage values
pare assumed to be between 0 and 100. - Fixed-amount discounts
dare assumed to be non-negative. - The final payable total should never be negative; it must be clamped to 0 if a discount would result in a negative value.
- You must apply at most one promotion to the cart. Some carts may have no promotions.
Task
Given the list of item prices, the shipping fee, and a list of promotions of the two types described above, write a function that performs the following:
- Computes the final payable amount for each available promotion.
- Identifies the promotion that yields the lowest payable amount (i.e., provides the maximum savings). This includes the option of applying no promotion if that results in the lowest total.
- Returns:
- The minimum payable amount after applying the best promotion (or no promotion).
- An indication of which promotion was chosen. You may assume each promotion has a unique identifier (e.g., an ID or index) and should define a reasonable representation for it.
Edge Cases to Handle
Your solution should robustly handle relevant edge cases, including but not limited to:
- An empty cart (no items).
- No promotions available.
- Zero or extremely small item prices or shipping fees.
- Promotions that do not improve the price (e.g., a 0% discount or a fixed discount of 0).
- Promotions that would otherwise drive the total below zero (ensuring the total is clamped to 0).
Specify the input and output formats for your function, including how you represent items and promotions, and then implement the function.
AI interview prep
Core dilemma
Achieving absolute precision and correctness in financial calculations involving multiple discount types and complex interaction rules, while optimizing for the best outcome among available options.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Compute cart total with best promotion
Checkout Price Calculator with Best Promotion
Assume you are implementing a checkout price calculator for an online shopping cart.
Input Data
You will be provided with:
- A list of items in the cart: Each item has a
price, represented as a non-negative decimal number. - A
shipping_fee: A non-negative decimal number representing the shipping cost for the entire order. - A list of promotions: Each promotion is one of two types:
- Percentage discount: Represented by a percentage value
p(e.g.,10for 10%). This discount applies only to the sum of item prices, not to the shipping fee. - Fixed-amount discount: Represented by a non-negative decimal
d. This discount is subtracted from the total amount including shipping.
- Percentage discount: Represented by a percentage value
Rules and Assumptions
- Let
items_sumbe the sum of all item prices. - Before any discount, the total amount is
items_sum + shipping_fee. - For a percentage discount
p(e.g., 10 for 10%):discounted_items = items_sum * (1 - p/100)total = discounted_items + shipping_fee
- For a fixed-amount discount
d:total = max(0, items_sum + shipping_fee - d)
- Percentage values
pare assumed to be between 0 and 100. - Fixed-amount discounts
dare assumed to be non-negative. - The final payable total should never be negative; it must be clamped to 0 if a discount would result in a negative value.
- You must apply at most one promotion to the cart. Some carts may have no promotions.
Task
Given the list of item prices, the shipping fee, and a list of promotions of the two types described above, write a function that performs the following:
- Computes the final payable amount for each available promotion.
- Identifies the promotion that yields the lowest payable amount (i.e., provides the maximum savings). This includes the option of applying no promotion if that results in the lowest total.
- Returns:
- The minimum payable amount after applying the best promotion (or no promotion).
- An indication of which promotion was chosen. You may assume each promotion has a unique identifier (e.g., an ID or index) and should define a reasonable representation for it.
Edge Cases to Handle
Your solution should robustly handle relevant edge cases, including but not limited to:
- An empty cart (no items).
- No promotions available.
- Zero or extremely small item prices or shipping fees.
- Promotions that do not improve the price (e.g., a 0% discount or a fixed discount of 0).
- Promotions that would otherwise drive the total below zero (ensuring the total is clamped to 0).
Specify the input and output formats for your function, including how you represent items and promotions, and then implement the function.