Nested Recipe Cart Transactions
Problem statement
Maintain a cooking-app cart containing at most one copy of each named recipe. Parallel arrays recipeNames and recipeIngredients give the catalog. Each selected recipe contributes one unit of every ingredient in its list.
Discount rows are given by discountedIngredients, discountThresholds and discountAmounts. An ingredient may have several tiers. For each ingredient, select the row with the greatest threshold not exceeding its current count, and contribute that row's amount exactly once. If no tier qualifies, it contributes 0. Sum these per-ingredient discounts. Row order and the relative amounts do not change which threshold wins.
The cart starts empty. Process these operation rows in order:
["add_recipe", recipeName]adds a catalog recipe; if already present, do nothing.["remove_recipe", recipeName]removes it; if absent, do nothing.["get_total_discount"]appends the current total discount. The existing plural tagget_total_discountsis an equivalent query alias.["begin"]starts a nested transaction scope.["commit"]closes the innermost scope; its changes remain part of its parent when there is one.["abort"]restores the working cart to the state at the innermost begin, then closes that scope.
All discount queries see the current working cart, including uncommitted changes. An outer abort also reverses changes from inner scopes committed into that outer scope. An inner abort preserves earlier outer changes. Commit and abort without an open scope are no-ops. Operations may end with scopes still open.
Return query results in encounter order. Mutations and history-control operations produce no result.
Function
processCartTransactions(recipeNames: String[], recipeIngredients: String[][], discountedIngredients: String[], discountThresholds: int[], discountAmounts: int[], operations: String[][]) → int[]Examples
Example 1
recipeNames = ["A","B","C"]recipeIngredients = [["Garlic"],["Garlic","Rice"],["Chicken"]]discountedIngredients = ["Garlic","Chicken"]discountThresholds = [2,1]discountAmounts = [4,3]operations = [["add_recipe","A"],["begin"],["add_recipe","B"],["get_total_discounts"],["abort"],["get_total_discounts"],["begin"],["add_recipe","C"],["commit"],["get_total_discounts"]]return = [4,0,3]Adding B inside the first transaction makes the Garlic rule qualify, but abort restores the cart to just A. The second transaction commits C, so the Chicken rule contributes 3.
Example 2
recipeNames = ["A","B","C"]recipeIngredients = [["Garlic"],["Garlic"],["Chicken"]]discountedIngredients = ["Garlic","Chicken"]discountThresholds = [2,1]discountAmounts = [5,2]operations = [["add_recipe","A"],["begin"],["add_recipe","B"],["begin"],["add_recipe","C"],["get_total_discounts"],["commit"],["get_total_discounts"],["abort"],["get_total_discounts"]]return = [7,7,0]The inner scope adds C, making both rules qualify. Committing it preserves that change inside the outer scope. Aborting the outer scope then discards both B and the committed inner change, leaving only A.
Example 3
recipeNames = ["A","B"]recipeIngredients = [["Chicken"],["Garlic"]]discountedIngredients = ["Chicken","Garlic"]discountThresholds = [1,1]discountAmounts = [2,3]operations = [["begin"],["add_recipe","A"],["begin"],["add_recipe","B"],["get_total_discounts"],["abort"],["get_total_discounts"],["commit"],["begin"],["remove_recipe","A"],["get_total_discounts"],["abort"],["get_total_discounts"],["abort"]]return = [5,2,0,2]Aborting the inner scope removes only B. The outer commit keeps A. A later transaction temporarily removes A, then abort restores it; the final abort has no active scope and does nothing.
Example 4
recipeNames = ["Chicken Parm","Stir Fry","Soup","Salad"]recipeIngredients = [["Garlic"],["Garlic"],["Garlic"],["Garlic"]]discountedIngredients = ["Garlic","Garlic"]discountThresholds = [2,4]discountAmounts = [3,8]operations = [["begin"],["add_recipe","Chicken Parm"],["add_recipe","Stir Fry"],["begin"],["add_recipe","Soup"],["add_recipe","Salad"],["get_total_discounts"],["commit"],["abort"],["get_total_discount"]]return = [8,0]The working cart contains four Garlic recipes inside the inner scope, so the discount is8. Committing that scope joins its parent; aborting the parent restores the empty cart and discount 0.
Constraints
1 <= recipeNames.length == recipeIngredients.length <= 2000. Recipe names are unique nonempty strings.1 <= recipeIngredients[i].length <= 20. Ingredient labels are strings and occur at most once per recipe.0 <= discountedIngredients.length == discountThresholds.length == discountAmounts.length <= 2000. Every discounted ingredient occurs in the catalog, and each (ingredient,threshold) pair is unique.1 <= discountThresholds[i] <= 10^9.1 <= discountAmounts[i] <= 10^6, and their sum is at most10^9.1 <= operations.length <= 2000. Rows have exactly the stated tags and arities, and every mutation names a known catalog recipe.- Active transaction depth never exceeds 100.
- At least one row is a discount query.