Katkat

Shopping Cart Totals with Best-Offer Discounts and Aisle Ordering, Frozen Items Last

InstacartSoftware Engineer · Technical Screen

Last updated

A practical coding exercise that parses shopping-cart item records and reports the total item count and price, then adds percentage discounts and buy-N-get-M-free offers, applying the cheapest offer per product. A final part orders items by aisle with frozen items last, testing parsing, exact money arithmetic and extensible code.

Write a program for a grocery shopping cart. It reads a list of item records for the products in the cart and reports the total number of items and the total price. The task then adds promotions and an ordering requirement for the items. Reading the problem and processing the input took a large share of the time in this round, so settle the input format early.

The record format was not reported; the illustration below is an assumption to make the parts concrete.

records (one line per product; illustrative format)
 milk,3.49,2,4,no name, unit price, quantity, aisle, frozen
 pizza,6.00,3,9,yes
 apples,0.50,6,1,no

promotions (illustrative)
 milk 10% off
 pizza buy 2 get 1 free
 pizza 25% off

Clarifying Questions

  • What does one record look like, which fields can be missing or malformed, and can the same product appear in several records?
  • Are prices given in dollars and cents, and where is a percentage discount rounded: per unit, per product line, or on the total?
  • Which units count toward the total number of items: does a free unit count?
  • Within one aisle, and among the frozen items, in what order should items appear?

Part 1 — Item count and total price

Parse the records and compute the total number of items in the cart and the total price.

What This Part Should Cover

  • Parsing and validating records into a typed item model
  • An exact money representation and the two totals
  • Handling repeated products and malformed records

Part 2 — Promotions

Add two kinds of promotion: a percentage discount, and "buy N, get M free". When more than one promotion applies to the same product, apply only the one that gives that product the lowest price.

Clarifying Questions for this Part

  • For buy N get M free, how are units beyond the last complete group treated, for example 5 units under buy 2 get 1 free?
  • Does each promotion apply to one product, or can one promotion cover several products?

What This Part Should Cover

  • The correct price under each promotion type, including quantities that are not a multiple of the group size and the rounding of percentages
  • Choosing the cheapest promotion per product, and reporting the discount
  • A structure in which a new promotion type can be added without changing the totals logic

Part 3 — Order by aisle, frozen items last

Return the cart's items ordered by aisle, with frozen items placed after all the other items.

What This Part Should Cover

  • A single sort with a composite key that puts frozen items last and orders by aisle
  • A deterministic order for ties, and a place for items with a missing aisle
  • The complexity of the ordering step

Hints

Hint 1

Clean model first: Separate turning raw records into typed values from the arithmetic, and choose a representation for money that stays exact when many prices are added together.

Hint 2

Compare outcomes, not labels: For one product, compute what the customer would pay under each applicable promotion, then compare those amounts rather than the promotions' headline numbers.

Hint 3

One key, two levels: Decide which comparison settles the order first and which second, and how items that tie on both should be ordered so that the output is deterministic.

Follow-up questions

  • Add a promotion across products, such as any 3 items from a group for a fixed price. Why is choosing the cheapest offer per product no longer enough?
  • Allow a percentage discount to stack with buy N get M. In which order do you apply them, and can the order change the price?
  • Some records have no aisle. Where do those items go in the order, and how do you report them?
  • How would you test the promotion logic so that adding a new promotion type cannot silently change the price of an existing one?