SOLID Principles · Coding problem
Add Campaigns Without Touching Checkout
A free coding problem from the Open/Closed Principle skill. Read it here, then solve it in the app in Java or Python.
- Easy
- Coding problem
- Free
- Java and Python
The problem
Marketing launches new discount campaigns all the time. The old checkout had a switch over campaign names that had to be edited - and re-tested - for every one. It has been redesigned around an extension point: each campaign is a DiscountRule, and Checkout simply looks the rule up. SummerSale (10% off) already exists.
Add three campaigns without changing Checkout:
BLACK_FRIDAY- 30% off orders of 2000 or more; nothing off smaller ordersLOYALTY- 5% off, but never more than 200 offFIRST_ORDER- a flat 100 off (Checkoutalready makes sure a price never goes below 0)
Percentages are rounded down (amount * 30 / 100). Register each new rule in Campaigns.all() - the only existing code you should need to edit. An unknown campaign gives no discount.
In Python: DiscountRule is an abstract base class, the registration list is the all_campaigns() function (Java's Campaigns.all()), and percentages round down with //.
Input: each command is <CAMPAIGN> <amount>, printed as <CAMPAIGN> <amount> -> <final price>.
Example
Input:
5
SUMMER 1000
BLACK_FRIDAY 2500
BLACK_FRIDAY 1999
LOYALTY 1000
FIRST_ORDER 60
Output:
SUMMER 1000 -> 900
BLACK_FRIDAY 2500 -> 1750
BLACK_FRIDAY 1999 -> 1999
LOYALTY 1000 -> 950
FIRST_ORDER 60 -> 0
Constraints
- At most 1000 commands;
0 <= amount <= 10^9; campaign names are case-sensitive.
How it's graded
- Run checks your code against the examples. Submit also runs hidden tests, and you see pass or fail for them, never their inputs.
- Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
- Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.
More problems in Open/Closed Principle
Solve it Add Campaigns Without Touching Checkout free
No card needed. Lessons, practice questions and easy problems are free forever.
Free plan, no card · 7-day Pro trial · Cancel anytime