Compute cart total with best promotion
Company: DoorDash
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates numerical reasoning for monetary calculations, handling of multiple promotion types, precision and rounding considerations, and edge-case handling such as empty carts and clamping totals within the Coding & Algorithms domain.
Constraints
- 0 <= len(prices); each price is a non-negative decimal.
- shipping_fee >= 0.
- 0 <= p <= 100 for percentage promotions; d >= 0 for fixed promotions.
- At most one promotion may be applied; applying none is always allowed.
- The payable amount is clamped to a minimum of 0.
Examples
Input: ([10.0, 20.0, 30.0], 5.0, [("percent", 10), ("fixed", 8.0)])
Expected Output: (57.0, 1)
Explanation: items_sum=60, base=65. percent 10 -> 60*0.9+5 = 59. fixed 8 -> 65-8 = 57. Fixed wins at index 1.
Input: ([100.0], 0.0, [("percent", 50), ("fixed", 40.0)])
Expected Output: (50.0, 0)
Explanation: base=100. percent 50 -> 50. fixed 40 -> 60. Percent wins at index 0.
Input: ([], 5.0, [("percent", 100), ("fixed", 3.0)])
Expected Output: (2.0, 1)
Explanation: Empty cart: items_sum=0, base=5. percent 100 -> 0*0+5 = 5 (shipping is never discounted). fixed 3 -> 2. Fixed wins.
Input: ([20.0, 30.0], 10.0, [])
Expected Output: (60.0, -1)
Explanation: No promotions, so the best is just base_total=60 with index -1.
Input: ([5.0], 2.0, [("percent", 0), ("fixed", 0.0)])
Expected Output: (7.0, -1)
Explanation: Both promotions are no-ops (7 each), neither strictly beats base_total=7, so no promotion is chosen (-1).
Input: ([10.0], 5.0, [("fixed", 100.0)])
Expected Output: (0.0, 0)
Explanation: base=15, fixed 100 -> max(0, 15-100)=0. Clamped to 0; the promotion still wins at index 0.
Input: ([50.0, 50.0], 20.0, [("percent", 100), ("fixed", 50.0)])
Expected Output: (20.0, 0)
Explanation: items_sum=100, base=120. percent 100 -> 0 + 20 shipping = 20. fixed 50 -> 70. Percent wins; shipping is never discounted so it can't go below 20.
Input: ([], 0.0, [])
Expected Output: (0.0, -1)
Explanation: Fully empty order: total 0, no promotion (-1).
Hints
- Compute items_sum and base_total = items_sum + shipping_fee once. The 'no promotion' option gives base_total (clamped to 0) and is your starting best.
- A percentage promo only discounts items_sum, so its total is items_sum*(1 - p/100) + shipping_fee. A fixed promo discounts the whole base_total: base_total - d.
- Track the running minimum and the index that produced it. Keep best_index = -1 unless a promotion strictly beats the current best, so 0% / 0-amount no-op promotions correctly leave you with no promotion. Round to 2 decimals to avoid float noise.