KatInterview

State-Tracked Recipe Basket with Bulk Pricing

Data Structure DesignAuto-graded · 13 test cases
HardAuto-gradedData Structure Designdesign-dsarrayshashing

Last updated

Simulate a versioned basket engine that aggregates multi-recipe component quantities for bulk discount calculations, supporting mutations and rollback operations.

You are building an execution engine for an interactive meal plan basket. Users modify their basket by adding or removing recipes. Each recipe consists of a dictionary mapping item names to their required quantities.

Because different recipes can share the same item (e.g., both require "garlic"), the basket aggregates item totals across all active recipes to determine whether bulk tier pricing applies.

Pricing Rules

Every component in ingredient_catalog is configured with three properties:

  • base_price (float): Cost per unit when the aggregated quantity across all recipes is strictly less than bulk_threshold.
  • bulk_threshold (int): Minimum total count of this item needed in the basket to qualify for bulk pricing.
  • bulk_price (float): Cost per unit when the aggregated quantity across all recipes is $\ge$ bulk_threshold.

Versioning Rules

  1. The basket starts at version 0 (empty basket).
  2. Every successful add_recipe or remove_recipe operation creates a new incremented version ID (1, 2, 3, ...).
  3. Failed additions or removals (e.g., adding a recipe already present, or removing a non-existent recipe) return -1 and do not generate a new version.
  4. get_total_cost computes the monetary value of a specified version without altering state. If no version is specified (or None is passed), it computes the price for the latest active version.
  5. checkout rolls back the state to the requested version ID: any versions created after that target version are permanently discarded, and the target version becomes the current head version. It returns the calculated cost at that restored state.
  6. If a query references an invalid version ID (e.g., negative, non-integer, or strictly greater than the highest current version ID), the method records the string 'ValueError' as its output instead of throwing an unhandled runtime error.

Input Parameters

  • ingredient_catalog: A dict mapping item names to their pricing dictionary ({"base_price": float, "bulk_threshold": int, "bulk_price": float}).
  • operations: A list of operation strings, where each entry is one of "add_recipe", "remove_recipe", "get_total_cost", or "checkout".
  • arguments: A list of argument lists corresponding to each operation:
    • "add_recipe": [recipe_id: str, ingredients: dict[str, int]]
    • "remove_recipe": [recipe_id: str]
    • "get_total_cost": [version_id: int] or [] (defaults to latest version if omitted or [None])
    • "checkout": [version_id: int]

Constraints

  • $0 \le \text{len(operations)} \le 1000$
  • Full copying (deep snapshotting) of the basket state on every single version mutation is avoided; version tracking should store structural deltas or efficient logs.
  • Item quantities inside recipes are positive integers.

Examples

Example 1

Input: versioned_recipe_cart({'garlic': {'base_price': 1, 'bulk_threshold': 5, 'bulk_price': 0.6}, 'chicken': {'base_price': 8, 'bulk_threshold': 2, 'bulk_price': 7}}, ['add_recipe', 'get_total_cost', 'add_recipe', 'get_total_cost', 'checkout', 'get_total_cost'], [['curry', {'garlic': 3, 'chicken': 1}], [1], ['roast', {'garlic': 3, 'chicken': 1}], [], [1], [2]])

Output: [1, 11, 2, 17.6, 11, 'ValueError']

Hints

Hint 1

Avoid deep-copying the entire basket map on every operation to satisfy memory efficiency expectations.

Hint 2

Track an ordered list of changes (deltas) where each entry records a recipe addition or removal relative to the previous version.

Hint 3

When rolling back via `checkout(v)`, truncate your change log so that version `v` becomes the latest point, discarding subsequent change records.