KatInterview

Cheapest Cart Total with Percent-Off and Buy-X-Get-Y Promotions

Math & Bit ManipulationModel answer
MediumModel answerMath & Bit Manipulationpricingdiscountsrequirements-clarification

Last updated

Define pricing rules for percent-off and buy-X-get-Y promotions, then build an algorithm that returns the lowest legal total for a cart, covering stacking, repeats, and rounding.

You are asked to build a pricing routine for a checkout system. The input is a list of purchased line items (product, unit price, quantity) and a set of promotions. The routine must return the lowest total the customer is allowed to pay.

Two kinds of promotions exist:

  • Percentage promotions: take a given percentage off an eligible amount.
  • Buy-X-get-Y promotions: when a customer buys X units of an eligible product, Y additional units are free.

The problem deliberately leaves many rules open. The schema of the records, which items a promotion applies to, whether promotions can be combined, how often one can be applied, which items become free when prices differ, and when rounding occurs are all unspecified.

What is expected:

  1. List the clarifying questions you would ask and state the assumptions you choose for each one. You should not claim a single "lowest price" until these are settled.
  2. Describe a data model for the cart and for the promotions.
  3. Give an algorithm that, under your stated assumptions, really does find the minimum price (not just a plausible price).
  4. Explain how you handle quantity thresholds, interactions between promotions, and rounding.
  5. Describe how you would test the result for correctness and reproducibility.

Code is welcome but not required. A precise, well-justified procedure matters more than a particular language.

Hints

Hint 1

Before designing anything, pin down semantics: does 'buy X get Y' mean X paid plus Y free per group of X+Y units, can it repeat, and which units are the free ones when prices differ?

Hint 2

Separate the problem into eligibility (which promotions can touch which units) and evaluation (what each valid combination costs). If stacking is allowed, the order of percent-off versus free-unit promotions changes the result, so you must either fix an order or search over orders.

Hint 3

If promotions cannot stack and each product line is independent, the optimum is simply the minimum over the options per line. If they can stack across shared units, think about enumerating a small set of candidate plans, or a greedy rule you can justify with an exchange argument. Use integer minor units (cents) and round at one defined step.