Apply Item Coupons with Exact Per-Item Cent Rounding
Company: Palantir
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Online Assessment
Overview: Calculate cart totals from item coupons with exact half-even cent rounding, unmatched-coupon handling, and integer monetary totals.
Read the full Palantir Software Engineer interview experience this question came from
Constraints
- Both arrays may be empty and have at most 100000 entries each.
- Each item is a pair [name, price]; each discount is a pair [name, percent_off].
- Names are nonempty ASCII strings of length at most 100.
- 0 <= price <= 1000000000.
- 0 <= percent_off <= 100, and percent_off is an integer.
- There is at most one coupon per item name; matching is exact and case-sensitive, and unmatched coupons are ignored.
- This initial version has no 80-percent cap.
- Cart totals may exceed signed 32-bit range (use long in Java and long long in C++).
- Prices and all returned monetary amounts are integer cents; the three returned keys are exactly subtotal, total_discount and final_total.
Examples
Input: ([], [])
Expected Output: {'subtotal': 0, 'total_discount': 0, 'final_total': 0}
Explanation: Minimum valid input: an empty cart and no coupons give three zeros.
Input: ([], [['Bread', 50]])
Expected Output: {'subtotal': 0, 'total_discount': 0, 'final_total': 0}
Explanation: An empty cart with a coupon: the coupon matches no row and is ignored.
Hints
- Keep the matching and the rounding steps separate: first find the coupon that belongs to an item's exact name, then calculate and round that one item's final discount exactly once.
- Each matched row is rounded on its own and only the rounded amounts are summed; rounding the cart-wide total instead can give a different answer, so decide which quantity you are rounding before you write the loop.
- The statement forbids letting binary floating-point error settle an exact half-cent tie, and every input value is an integer, so an exact treatment of price * percent_off / 100 is available.