KatInterview

Find Optimal Discount from Multiple Promotion Types

Arrays & HashingAuto-graded · 17 test cases
MediumAuto-gradedArrays & Hashingsimulationgreedymath

Last updated

Tests ability to compare multiple discount strategies with different application rules and select the best outcome while handling edge cases like empty inputs and clamping.

You are building a pricing engine for an e-commerce platform that needs to determine the final checkout amount after applying the most beneficial discount.

You receive three inputs:

  1. A list of item prices (each a non-negative number)
  2. A shipping fee (a non-negative number)
  3. A list of available discounts

There are two types of discounts:

  • Percentage discounts: Apply only to the merchandise subtotal (sum of item prices), not to shipping. Represented with "type": "percentage" and a "value" between 0 and 100.
  • Fixed-amount discounts: Subtract a dollar amount from the combined merchandise and shipping total. Represented with any other type (typically "type": "fixed") and a non-negative "value".

Discount Application Rules:

  • You may apply at most one discount to an order
  • Applying no discount is always a valid option
  • For a percentage discount with value p: the merchandise subtotal is multiplied by (1 - p/100), then shipping is added at full price
  • For a fixed discount with value d: subtract d from the sum of merchandise and shipping
  • The final amount can never be negative; if any calculation yields a negative result, return 0 instead

Your Task:

Write a function that computes which discount (if any) produces the lowest final amount, and return both that minimum amount and an identifier for the chosen discount.

Return a dictionary with two keys:

  • "total": the minimum achievable payment amount
  • "promotion_index": the zero-based index of the selected discount in the input list, or None if no discount was applied

When multiple options produce the same final amount, prefer the earlier option (lower index), or None if the no-discount option ties for best.

Constraints:

  • 0 ≤ number of items ≤ 1000
  • 0 ≤ each item price ≤ 10000
  • 0 ≤ shipping fee ≤ 1000
  • 0 ≤ number of discounts ≤ 100
  • For percentage discounts: 0 ≤ value ≤ 100
  • For fixed discounts: 0 ≤ value ≤ 100000
  • All prices and fees are floating-point numbers

Examples

Example 1

Input: find_best_discount([20, 30], 5, [{'type': 'percentage', 'value': 10}, {'type': 'fixed', 'value': 8}])

Output: {'total': 47, 'promotion_index': 1}

Example 2

Input: find_best_discount([], 5, [{'type': 'fixed', 'value': 10}])

Output: {'total': 0, 'promotion_index': 0}

Example 3

Input: find_best_discount([10], 2, [])

Output: {'total': 12, 'promotion_index': None}

Hints

Hint 1

Calculate the base total (items plus shipping) first, then treat it as one candidate alongside all discounts.

Hint 2

Remember that percentage discounts change only the item subtotal, while fixed discounts reduce the complete total including shipping.

Hint 3

Use strict inequality when comparing totals so that a discount must actually improve the price to be selected over the no-discount baseline.