KatInterview

Build an Inventory Manager with Nested Recipe Expansion

Data Structure DesignModel answer
EasyModel answerData Structure Designobject-oriented-designgraph-validationatomicity

Last updated

Implement a service that models recipes with nested dependencies, validates acyclic graphs, and atomically decrements raw inventory when orders succeed.

You are building an inventory management system for a kitchen service. The system tracks recipes, raw materials, and fulfillment of orders. Each recipe has a name, a preparation duration, and a list of ingredients with quantities. An ingredient can be either a raw material or another recipe, allowing nested composition.

When an order is placed for a recipe, the system must:

  • Recursively expand all nested recipes to determine the total raw materials needed
  • Verify that sufficient inventory exists for every raw material
  • Atomically decrement all required inventory counts, or leave inventory unchanged if any requirement cannot be met

Your task is to design the core data structures and methods for this system, addressing the following:

Part 1: Core Model

Define classes and methods to represent recipes, raw materials, and inventory. Your design should support:

  • Adding a new recipe with a name, preparation time (in minutes), and ingredient list
  • Adding or updating raw material inventory counts
  • Placing an order for a recipe by name
  • Querying recipes by preparation time threshold

Specify how your design handles:

  • Unique naming for recipes and materials
  • Validation of quantities (recipes require positive ingredient amounts; inventory is non-negative)
  • Distinguishing between raw materials and recipe references in ingredient lists
  • Behavior when a recipe or material name already exists

Part 2: Dependency Resolution

Explain how your system expands nested recipes and detects invalid states:

  • Identifying missing recipe or material references
  • Detecting cycles in the recipe dependency graph (direct or indirect)
  • Aggregating quantities when the same raw material appears through multiple paths
  • Handling integer overflow when multiplying or summing quantities

Part 3: Atomic Order Fulfillment

Design the order placement logic to ensure atomicity:

  • Calculate total raw material requirements before modifying inventory
  • Ensure all-or-nothing updates: either all inventory decrements succeed or none do
  • Handle concurrent order requests safely
  • Return success or failure with clear semantics

Part 4: Query Interface and Testing

Define query methods and describe key test scenarios:

  • List recipes with preparation time above a given threshold (specify whether inclusive or exclusive)
  • Ensure returned collections are immutable or defensive copies
  • Outline tests for nested expansion, cycle detection, exact inventory matches, insufficient stock, and concurrent orders

Hints

Hint 1

Use separate types or enums to distinguish raw material references from recipe references in ingredient lists.

Hint 2

Cycle detection can use depth-first search with a recursion stack, checking each recipe only once per traversal.

Hint 3

Expand the full raw material requirement map before checking any inventory levels, then verify all counts before decrementing any.