cswork / libraryOpen practice workspace

Coding questions · Instacart

Calculate Inventory Purchases with Buy-X-Get-Y-Free Coupons

Calculate exact-cent inventory totals with stock checks and a clearly defined buy-X-get-Y-free coupon, including partial groups and two-decimal output.

Compute the total price of a requested quantity of one inventory item, then support an optional buy-X-get-Y-free coupon for that item.

Implement purchase_total(inventory: string[][], item: string, quantity: int, buy: int, free: int) -> string.

Constraints & Assumptions

These coupon and error semantics are explicit practice choices where the report is incomplete. Quantity is the total number of units the customer receives, including any free units. For each complete group of buy + free units, charge for buy; any remaining units are charged up to buy. Thus with buy 5/get 2, quantities 3, 6, and 7 pay for 3, 5, and 5 units respectively. No free units are added beyond the requested quantity.

  • Each inventory row is [name,stock,unitPrice]; names are unique nonempty ASCII identifiers. Stock is a nonnegative integer string. Unit price is a nonnegative decimal string with exactly two digits after the point.
  • At most 1000 inventory rows, stock and quantity at most 1000000, and unit price at most 1000000.00. Quantity is nonnegative.
  • buy == 0 and free == 0 means no coupon. Otherwise 1 <= buy <= 1000000 and 0 <= free <= 1000000.
  • Return UNKNOWN_ITEM if the item does not exist. Otherwise return OUT_OF_STOCK if quantity exceeds stock. No inventory mutation occurs.
  • On success return the total with exactly two fractional digits and no currency symbol. Parse and calculate using integer cents, not binary floating point.

Examples

inventory = [["A","50","0.25"],["B","10","1.50"]]
item = "A", quantity = 7, buy = 5, free = 2
result = "1.25"

For the same item, quantity 3 costs 0.75; quantity 0 costs 0.00; quantity 51 returns OUT_OF_STOCK. Without a coupon, seven units cost 1.75.

Explain how the function avoids decimal rounding errors and why an incomplete qualifying purchase cannot receive the full advertised free quantity. In an interview, clarify whether quantity denotes paid units or received units before selecting the coupon formula.

Hint: Separate units from money

First determine how many of the received units are chargeable. Then multiply by an exact integer price and format the final cents.

Calculate Exact Inventory Purchase Totals with Coupons

Approach

Find the exact item name first; return UNKNOWN_ITEM if it is absent. For an existing item, compare requested received units with stock before pricing. Parse whole and fractional decimal components separately into integer cents. With no coupon, all quantity units are chargeable. Otherwise let g=buy+free. Each complete group contributes buy chargeable units, and the remaining r units contribute min(r,buy), so paid=floor(quantity/g)*buy+min(quantity%g,buy). This charges all units before the qualifying buy threshold, then only the requested portion of the available free units, without adding units beyond quantity. Multiplying paid by exact cents avoids decimal rounding. Format the integer quotient and two-digit remainder after dividing total cents by 100. The maximum total is 100000000000000 cents, which fits signed 64-bit integers and remains below JavaScript Number's exact-integer limit. Java and C++ therefore use wide arithmetic; JavaScript only converts integer decimal pieces, never the whole fractional price. No inventory entry is modified. Scanning n inventory rows costs O(n) name comparisons plus string-character work; fixed numeric bounds keep price arithmetic constant-sized. The C++ value-parameter copy is input storage rather than auxiliary traversal state.

  • Time complexity: O(n) row comparisons plus name-character work
  • Space complexity: O(1) auxiliary state under numeric bounds, excluding input copies