Problem: Calculate Prices for Experiment Groups
Problem statement
You’re rolling out a real-time A/B pricing experiment for your e-commerce platform. Users are automatically assigned to different experiment groups to test new pricing strategies:
Group A (Conservative): Small 5% markup to maintain conversion rates.
Group B (Aggressive): 25% markup to test premium positioning.
Group C (Dynamic): Algorithmic pricing that fluctuates based on simulated demand.
A central experiment service determines the user’s assigned group, for example:
getExperimentGroup(userId) → 'A' | 'B' | 'C'
Pricing experiments often rely on nested conditionals to handle different pricing models, which quickly become unmanageable as experiments evolve. A typical implementation is as follows:
const group = getExperimentGroup(userId);if (group === 'A') { /* conservative logic */ }else if (group === 'B') { /* aggressive logic */ }else if (group === 'C') { /* dynamic logic */ }
It works—but every experiment update means changing core code. You’ll now design a pricing system using the Strategy Pattern, so each pricing model is encapsulated and selected dynamically at runtime.
Goal
Implement both sides of the pattern:
Strategies (Pricing algorithms):
ConservativePricing: Adds 5% markup.AggressivePricing: Adds 25% markup.DynamicPricing: Randomly adds 0–50% markup.Each defines
.calculate(basePrice)and returns a two-decimal formatted number.
Context (PricingService):
Delegates
.calculate()to the injected strategy.Adds a
.setStrategy()method for runtime swapping.
Experiment logic:
Implement
getExperimentGroup(userId)that assigns groups randomly.Use it to dynamically select and inject the correct pricing strategy.
Constraints
No
iforswitchinsidePricingService.Strategy selection should happen externally (via a simple mapping or factory).
Prices must be returned as numbers with 2 decimal places.
DynamicPricingshould produce different results on multiple runs.
Sample output
The examples below illustrate what the output should look like:
const userId = Math.floor(Math.random() * 1000);const basePrice = 100;const group = getExperimentGroup(userId);const strategy = selectStrategyForGroup(group);const service = new PricingService(strategy);console.log(`User ${userId} assigned to Group ${group}`);console.log('Final price:', service.calculate(basePrice));/* Expected output (varies due to randomization):User 517 assigned to Group CFinal price: 138.42*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.
Problem: Calculate Prices for Experiment Groups
Problem statement
You’re rolling out a real-time A/B pricing experiment for your e-commerce platform. Users are automatically assigned to different experiment groups to test new pricing strategies:
Group A (Conservative): Small 5% markup to maintain conversion rates.
Group B (Aggressive): 25% markup to test premium positioning.
Group C (Dynamic): Algorithmic pricing that fluctuates based on simulated demand.
A central experiment service determines the user’s assigned group, for example:
getExperimentGroup(userId) → 'A' | 'B' | 'C'
Pricing experiments often rely on nested conditionals to handle different pricing models, which quickly become unmanageable as experiments evolve. A typical implementation is as follows:
const group = getExperimentGroup(userId);if (group === 'A') { /* conservative logic */ }else if (group === 'B') { /* aggressive logic */ }else if (group === 'C') { /* dynamic logic */ }
It works—but every experiment update means changing core code. You’ll now design a pricing system using the Strategy Pattern, so each pricing model is encapsulated and selected dynamically at runtime.
Goal
Implement both sides of the pattern:
Strategies (Pricing algorithms):
ConservativePricing: Adds 5% markup.AggressivePricing: Adds 25% markup.DynamicPricing: Randomly adds 0–50% markup.Each defines
.calculate(basePrice)and returns a two-decimal formatted number.
Context (PricingService):
Delegates
.calculate()to the injected strategy.Adds a
.setStrategy()method for runtime swapping.
Experiment logic:
Implement
getExperimentGroup(userId)that assigns groups randomly.Use it to dynamically select and inject the correct pricing strategy.
Constraints
No
iforswitchinsidePricingService.Strategy selection should happen externally (via a simple mapping or factory).
Prices must be returned as numbers with 2 decimal places.
DynamicPricingshould produce different results on multiple runs.
Sample output
The examples below illustrate what the output should look like:
const userId = Math.floor(Math.random() * 1000);const basePrice = 100;const group = getExperimentGroup(userId);const strategy = selectStrategyForGroup(group);const service = new PricingService(strategy);console.log(`User ${userId} assigned to Group ${group}`);console.log('Final price:', service.calculate(basePrice));/* Expected output (varies due to randomization):User 517 assigned to Group CFinal price: 138.42*/
Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.