Process Delimited Product, Promotion, and Shelf Records
Company: Instacart
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: hard
Interview Round: Onsite
## Process Delimited Product, Promotion, and Shelf Records
You are given product records such as `a100|apple|6|123`, where the fields are SKU, name, quantity, and unit price in cents. Later parts add promotions such as `a100|pct|20` or `a100|bxyf|1|2`, and enrich products with aisle and frozen fields such as `a100|apple|6|123|4|false`.
The original exercise contains ambiguous wording about invalid negative values, percentage rounding, the meaning of `buy x, y free`, and the shelf-order rule. Identify and resolve those ambiguities before implementation.
### Part 1 — Parse Products and Compute Total Cost
Parse every row, validate its field count and types, skip records containing an invalid negative quantity or price under the agreed contract, and compute the total cost in cents.
#### What This Part Should Cover
- One parser that returns a typed record or a specific validation error.
- Checked integer multiplication and addition.
- An explicit distinction between skipping an invalid row and terminating the whole scan.
- Tests for zero, negative, malformed, duplicate-SKU, and large values.
```hint Parse before calculating
Do not let partially parsed numeric fields enter the total before the entire row has passed validation.
```
### Part 2 — Apply the Cheapest Promotion
Parse percentage and buy-X-get-Y-free promotions. For each product, compare every applicable promotion and use the one producing the lowest final item total; then return the total discounted price across products, not the amount saved.
#### What This Part Should Cover
- Exact percentage rounding in integer cents.
- A written definition of which units are paid for in a buy-X-get-Y-free group.
- Baseline full price as an option so a malformed or harmful promotion cannot increase cost.
- Multiple promotions, remainders, missing SKUs, and duplicate promotion records.
```hint Compare final prices in one unit
Compute a candidate price in cents for each promotion, then choose the minimum rather than mixing savings and payable totals.
```
### Part 3 — Order Shelves with Frozen Items Last
Use the enriched records to produce a shelf traversal that always places frozen goods last. The examples imply that the frozen shelf is guaranteed to be at one end: traverse aisles ascending when frozen goods occupy the highest aisle, and descending when they occupy the lowest aisle. Confirm this interpretation and define ties before coding.
#### What This Part Should Cover
- Validation that frozen records occupy the declared extreme shelf.
- Direction chosen from that extreme rather than one hard-coded sort order.
- Frozen items last with deterministic ordering inside an aisle.
- Explicit behavior for no frozen goods, several frozen goods, or a violated guarantee.
```hint Choose direction from the cold end
If the frozen shelf is one physical endpoint, begin at the other endpoint so frozen goods are encountered last.
```
### What a Strong Answer Covers
- Centralized typed parsing instead of repeated string indexing.
- Exact money arithmetic and an unambiguous promotion contract.
- The final payable total, with no mutation from rejected rows.
- A shelf-order rule that explains both ascending and descending examples.
### Follow-up Questions
1. How would you report invalid rows without losing valid ones?
2. What rounding rule would you expose to users for percentage discounts?
3. Can two promotions ever be combined, and how would that change the search space?
4. How would you preserve source order for several products on one aisle?
Quick Answer: Parse delimited product records, calculate prices under competing promotions, and order shelves so frozen goods come last. The exercise tests typed validation, exact integer money arithmetic, ambiguous business-rule clarification, deterministic ordering, and robust handling of malformed records.