← 返回 instacart 的题目列表Onsite Coding: Product Pricing, Promotions & Shelf Ordering
类型:qbank
Parse pipe-delimited product records across three tasks: compute total merchandise cost, apply the cheapest percentage or buy-X-get-Y promotion per SKU, then order shelf records while placing frozen goods last. Negative-value handling, final-price semantics, and the third part's ordering rule require explicit clarification.
Requirements
The round uses pipe-delimited strings and has three largely independent parts.
Total merchandise cost
Each product row has the shape sku|name|quantity|unit_price_cents, for example a100|apple|6|123.
Parse the records and return the total cost across all valid products. Prices are expressed in cents.
Negative numeric values appear in tests. Confirm whether they invalidate the entire row before implementing the calculation.
Best promotion per SKU
A second string array describes promotions.
a100|pct|20 means 20% off for SKU a100.
a100|bxyf|1|2 means a buy-X-get-Y-free promotion with x = 1 and y = 2.
If an item has multiple promotions, use the one that produces the lowest final price.
Return the total post-discount price across all products, not the amount saved.
Shelf ordering with frozen products last
Product rows gain two fields: sku|name|quantity|unit_price_cents|aisle|is_frozen, such as a100|apple|6|123|4|false.
Order all products by aisle while ensuring frozen products appear last.
The input guarantees that the frozen aisle is at one extreme of the aisle range. Use the examples to confirm which sort direction applies.
Examples
The shelf-ordering examples produce these sequences:
apple (aisle 1), cake (aisle 3), frozen pizza (aisle 7)
Apple (aisle 7), candy (aisle 2), frozen cake (aisle 1)
Notes
The three parts are only loosely coupled, so expect substantial code volume and limited reuse beyond parsing and record modeling.
Keep pipe parsing and validation in one typed helper so the clarified negative-value policy is applied consistently in all three parts.
For a literal buy-x-get-y-free promotion and quantity q, each complete block of x + y units charges for x; the remainder charges for at most x units. Evaluate every promotion for a SKU and select the minimum resulting total. Confirm the percentage-discount rounding rule before implementing integer-cents arithmetic.
The written handling of negative values did not align cleanly with the judge behavior. Clarify whether a negative field means skipping the entire row.
The promotion result was initially described as the discount amount, then corrected to the total price after discounts. Restate the required output before coding.
For shelf ordering, infer direction from the guaranteed frozen extreme: sort aisles ascending when frozen goods occupy the maximum aisle and descending when they occupy the minimum aisle. Both supplied sequences then remain aisle-ordered with frozen goods last.
With promotions indexed by SKU, the first two parts are linear in the product and promotion records; shelf ordering is O(n log n) time for n products.
Preparation
Implement one typed parser that handles both the four-field and six-field product formats; test zero and negative numeric fields under each clarified invalid-row policy.
Write a table-driven promotion drill covering quantities below, equal to, and above x + y, then compare percentage and buy-X-get-Y-free candidates using a stated integer-cents rounding rule.
Re-derive the sort direction for both supplied shelf sequences, then implement the comparator without special-casing either concrete aisle number.