FastPrepProcess Shopping Records, Promotions, and Display Order
Problem · Array

Process Shopping Records, Promotions, and Display Order

Learn this problem
â—Ź HardInstacart logoInstacartFULLTIMEONSITE INTERVIEW

Problem statement

You receive shopping records in rows. A valid row has exactly six pipe-separated fields: sku|name|quantity|priceCents|aisle|frozen. The sku, name, and aisle fields are non-empty ASCII text without pipes; quantity and priceCents are nonnegative integers; and frozen is exactly true or false. Ignore every malformed row and every row with a negative quantity or price.

A valid promotion is one of sku|PERCENT|p, where 0 <= p <= 100, or sku|BUY_X_GET_Y|x|y, where x and y are positive. Ignore malformed promotions. For a percentage promotion, discount the full row price and round half up to the nearest cent. For a buy-X-get-Y-free promotion, each complete group of x + y units charges for only x units. Apply at most one promotion to each row, choosing the lowest valid row price; the undiscounted price is always available. Duplicate SKUs remain independent rows.

Return an array whose first entries are units=<totalQuantity>, base=$<dollars.cents>, and promoted=$<dollars.cents>. Append every valid original row after the summaries, with non-frozen rows first and frozen rows last. Within each group, sort by ascending aisle; exact aisle ties retain input order.

Function

processShoppingRecords(rows: String[], promotions: String[]) → String[]

Examples

Example 1

rows = ["a100|apple|6|123|produce|false","i200|ice cream|2|450|frozen|true","bad|row"]promotions = ["a100|PERCENT|10","a100|BUY_X_GET_Y|2|1"]return = ["units=8","base=$16.38","promoted=$13.92","a100|apple|6|123|produce|false","i200|ice cream|2|450|frozen|true"]

The malformed row is ignored. Apples cost 738 cents normally, 664 cents after 10% off, and 492 cents under buy-two-get-one-free, so the cheapest promotion wins. Ice cream has no promotion. The non-frozen row precedes the frozen row.

Example 2

rows = ["b|bread|1|199|bakery|false","m|milk|3|101|dairy|false","p|peas|0|75|a01|true"]promotions = ["m|PERCENT|50","m|BUY_X_GET_Y|1|1","m|PERCENT|101"]return = ["units=4","base=$5.02","promoted=$3.51","b|bread|1|199|bakery|false","m|milk|3|101|dairy|false","p|peas|0|75|a01|true"]

Half of milk's 303-cent row price rounds up to 152 cents, while buy-one-get-one-free charges 202 cents. The invalid 101% promotion is ignored. The zero-quantity peas row is valid and appears in the frozen group.

Constraints

  • 0 <= rows.length, promotions.length <= 100000.
  • The combined length of all row and promotion strings is at most 1000000.
  • Valid sku, name, and aisle fields contain printable ASCII characters other than |; sku and aisle contain no whitespace.
  • Valid quantities and prices are from 0 through 1000000000.
  • At most 20 valid promotions share one SKU.
  • The total quantity, base price, promoted price, and every intermediate product fit in a signed 64-bit integer.

More Instacart problems

drafts saved locally
public String[] processShoppingRecords(String[] rows, String[] promotions) {
    // Write your solution here.
}
rows["a100|apple|6|123|produce|false","i200|ice cream|2|450|frozen|true","bad|row"]
promotions["a100|PERCENT|10","a100|BUY_X_GET_Y|2|1"]
expected["units=8", "base=$16.38", "promoted=$13.92", "a100|apple|6|123|produce|false", "i200|ice cream|2|450|frozen|true"]
checking account