FastPrepNested Recipe Cart Transactions
Problems/Nested Recipe Cart Transactions

Nested Recipe Cart Transactions

Decagon logoDecagon● HardFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Decagon phone screen. The reports describe begin, commit, and abort nested inside each other, with staged changes that discount queries can see, and they link to the base recipe-cart question. That base uses the highest ingredient tier a cart qualifies for, and changes that are safe to repeat. An inner commit joins its parent scope, so an outer abort undoes it. We chose the finite limits and the batch interface. Our version is about 90% the same as the reported questions.

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 tag get_total_discounts is 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 is 8. 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 most 10^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.

More Decagon Phone Screen questions

  • Count Complete Tic-Tac-Toe GamesPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Rolling CSAT TrackerPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Batch API Requests with Retry DelaysPHONE SCREEN · Seen Sep 2026
  • Recipe Cart with Undo and RedoPHONE SCREEN · Seen Sep 2026
  • Recipe Shopping Cart DiscountsPHONE SCREEN · Seen Aug 2026
  • Versioned Recipe Shopping CartPHONE SCREEN · Seen Jul 2026
  • Count Hits in the Last Five MinutesPHONE SCREEN · Seen Apr 2026
  • Exclusive Function Execution TimePHONE SCREEN · Seen Mar 2026
  • Largest Rectangle in a HistogramPHONE SCREEN · Seen Dec 2024

Similar questions at other companies

  • Layer History with Batch Undo and RedoFigma · OA · PHONE SCREENFigma interview questions
  • Key-Value Store with Nested TransactionsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
  • Transactional In-Memory RedisHashiCorp · ONSITE INTERVIEWHashiCorp interview questions
  • Design In-Memory File SystemSnowflake · PHONE SCREENSnowflake interview questions
  • 15-Minute Chat Session TrackerOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Dynamic Least-Loaded Job DispatcherCresta · PHONE SCREENCresta interview questions
  • Sparse Vector and Matrix OperationsBloomberg LP · PHONE SCREENBloomberg LP interview questions
  • Capacity-Limited Timed CacheNetflix · PHONE SCREENNetflix interview questions
  • Tiered Expiring Item StoreHudson River Trading · PHONE SCREEN · ONSITE INTERVIEWHudson River Trading interview questions
  • LFU CacheFreshworks · PHONE SCREENFreshworks interview questions

Decagon interview experiences

  • Decagon Experienced SWE Phone Screen: 1 Experience
  • Decagon New Grad SWE Interviews: 1 Experience
All Decagon interview questionsSee Decagon hiring insights
Saved
class Solution {
    public int[] processCartTransactions(String[] recipeNames, String[][] recipeIngredients, String[] discountedIngredients, int[] discountThresholds, int[] discountAmounts, String[][] operations) {
        // Write your code here.
    }
}
Checking account…