Refactor a Checkout Discount Calculator to Honor Open Closed
Last updated: November 29, 2026
Quick Overview
A retail client's checkout has one long `if/elif` chain that computes discounts by promotion type. Refactor it so new promotion types can be added without editing the calculator, and make sure money math is exact.
EPAM Systems
November 29, 20260
0
0 solved
A retail client's checkout has one long `if/elif` chain that computes discounts by promotion type. Refactor it so new promotion types can be added without editing the calculator, and make sure money math is exact.
EPAM technical interviews lean hard on OOP fundamentals, SOLID and design patterns because engineers join existing client codebases and must extend them safely. This question checks whether you can name the smell, pick the Strategy pattern for the right reason, and avoid floating point in currency.
What the Interviewer Expects
- Identify the Open Closed violation and the growing conditional as the smell
- Introduce a rule interface (Strategy) with `applies` and `discount`
- Keep the calculator closed for modification: rules are injected
- Use `Decimal` and explicit rounding for money
- Define a clear combination policy (best single discount) and cap at subtotal
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
- How would you support stacking discounts with a priority order instead of best single?
- Where would rules come from in production: code, config, or a rules engine?
- How would you unit test a new rule in isolation?
- Which other SOLID principle does dependency injection of rules serve?
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
Promotions today: percent off a category, fixed amount off above a cart threshold. Tomorrow: buy-one-get-one, coupon codes. The business rule I would confirm: only the single best discount applies, an...
High-Level Design
The smell is a conditional that must be edited for every new promotion, which violates Open Closed and concentrates risk in one function. I would define an abstract DiscountRule with two methods and...