Write Property-Based Tests for an Amazon Cart Total Calculator
Last updated: November 29, 2026
Quick Overview
Given a function that computes a cart total from line items, a percentage coupon, and a tax rate in integer cents, write property-based tests that would catch rounding, ordering, and discount bugs that example-based tests miss.
Amazon
November 29, 20260
0
0 solved
Given a function that computes a cart total from line items, a percentage coupon, and a tax rate in integer cents, write property-based tests that would catch rounding, ordering, and discount bugs that example-based tests miss.
Pricing bugs at Amazon scale cost real money, and penny rounding differences between services cause reconciliation failures. The interviewer is checking whether you can state invariants of the domain, generate inputs that hit edge cases, and keep failures reproducible.
What the Interviewer Expects
- Uses integer cents rather than floats
- States properties like non-negativity, order independence, and monotonic discounts
- Includes an independent oracle computed a different way
- Seeds the generator so a failure can be replayed
- Explains shrinking and how a framework like Hypothesis helps
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- Which property would catch a bug where tax is rounded per line instead of once?
- How would you generate edge cases such as zero items or a 100 percent coupon on purpose?
- How do property tests fit alongside golden-file regression tests?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
cart_total(items, coupon_pct, tax_rate_bp)returns integer cents.- Coupon applies to the subtotal, then tax applies to the discounted amount; round half up once per stage.
- Tests must explore ma...
High-Level Design
Pick properties that must hold for every input: total is never negative; shuffling lines does not change it; increasing the coupon never raises the total; a 100 percent coupon yields zero; splitting a...