Problem · Array

Product Total With the Cheapest Promotion

Learn this problem
â—Ź MediumInstacart logoInstacartFULLTIMEONSITE INTERVIEW

Problem statement

Given product and promotion records, return the minimum total price in integer cents.

Each product record has the form sku|name|quantity|priceCents. Skip a product record exactly when quantity or priceCents is negative, and continue processing later records. Every other record contributes independently, including repeated SKUs.

Promotion records have either form:

  • sku|pct|p: charge floor(quantity * priceCents * (100 - p) / 100) cents for the full product record.
  • sku|bxyf|x|y: each complete group of x + y units contains y free units. The charged count is quantity - floor(quantity / (x + y)) * y.

A SKU has at most one promotion of each type. For every valid product record, choose the minimum of its regular price and every applicable promoted price. A product without a promotion uses its regular price.

Function

minimumProductTotal(products: String[], promotions: String[]) → long

Examples

Example 1

products = ["A|Apple|3|100","B|Bread|2|250","C|Bad|-1|900"]promotions = ["A|pct|20","A|bxyf|2|1","B|pct|10"]return = 650

For A, buy-two-get-one-free charges 200, cheaper than the 240 percentage price. B costs 450 after 10% off. The negative-quantity C record is skipped.

Example 2

products = ["X|Item|5|99","X|Again|2|99","Y|Free|0|500","Z|Skip|1|-5"]promotions = ["X|bxyf|2|1","Y|pct|100"]return = 594

The two X records are priced independently at 396 and 198. Zero quantity contributes zero, and the negative-price record is skipped.

Example 3

products = ["A|A|1|101","B|B|7|80","C|C|2|200"]promotions = ["A|pct|33","B|bxyf|2|1"]return = 867

The percentage price for A floors 67.67 to 67 cents. B charges five of seven units for 400, and unpromoted C costs 400.

Constraints

  • 0 <= products.length <= 10^5 and 0 <= promotions.length <= 2 * 10^5.
  • Every record is syntactically valid, and string fields do not contain |.
  • For percentage promotions, 0 <= p <= 100.
  • For buy-x-get-y-free promotions, x and y are positive.
  • Every intermediate line price and the final answer fit in a signed 64-bit integer.

More Instacart problems

drafts saved locally
public long minimumProductTotal(String[] products, String[] promotions) {
  // write your code here
}
products["A|Apple|3|100","B|Bread|2|250","C|Bad|-1|900"]
promotions["A|pct|20","A|bxyf|2|1","B|pct|10"]
expected650
checking account