PracHub
QuestionsLearningGuidesInterview Prep
|Home/Software Engineering Fundamentals/Decagon

Add Nested Begin, Commit, and Abort Semantics to a Shopping Cart

Last updated: Jul 22, 2026

Quick Overview

Extend an in-memory shopping cart with nested begin, commit, and abort operations while preserving correct state visibility. Compare snapshot and undo-based designs, define invalid-operation behavior, and test nested scope combinations, repeated item changes, discount caching, and future durability needs.

  • hard
  • Decagon
  • Software Engineering Fundamentals
  • Software Engineer

Add Nested Begin, Commit, and Abort Semantics to a Shopping Cart

Company: Decagon

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: hard

Interview Round: Technical Screen

# Add Nested Begin, Commit, and Abort Semantics to a Shopping Cart An in-memory shopping cart already supports `add(item)`, `remove(item)`, and a pure `discount(cart_state)` calculation. Extend it with transaction methods: - `begin()`: start a new transaction scope. - `commit()`: close the innermost active scope while preserving its changes. - `abort()`: close the innermost active scope and undo only changes made since its matching `begin()`. - `get_total_discount()`: calculate from the currently visible state, including uncommitted changes. Transactions can be nested. For example, after `begin(); add(A); begin(); add(B)`, aborting once removes only `B`; committing once preserves `B` inside the still-active outer scope. Aborting when no scope is active is a no-op. State and justify the behavior of `commit()` with no active scope. Operations performed outside a transaction take effect immediately. Design the data structures and implement the transaction layer. The item and discount representations may be treated as existing abstractions; focus on state semantics. ### Constraints & Assumptions - Calls are single-threaded for the base problem. - An item may have a quantity greater than one. - `remove` cannot produce a negative quantity; define whether it rejects or clamps an invalid request. - The cart can receive many operations between transaction boundaries. ### Clarifying Questions to Ask - Does nested `commit` merge into its parent or make changes irrevocable immediately? - Must an invalid `remove` fail atomically? - Is `get_total_discount()` pure, and may its result be cached? - Are transaction scopes required to survive process failure? ### What a Strong Answer Covers - Precise nested-scope semantics and visibility - A stack-based implementation using snapshots or reversible changes - Correct handling of repeated changes to the same item - Complexity and memory trade-offs - Tests for nested commit/abort combinations and invalid operations ### Follow-up Questions - How would you add thread safety? - How would the design change if transactions had to survive a crash? - Can an undo log avoid copying the full cart at every `begin()`? - When is cache invalidation for the discount total safe?

Quick Answer: Extend an in-memory shopping cart with nested begin, commit, and abort operations while preserving correct state visibility. Compare snapshot and undo-based designs, define invalid-operation behavior, and test nested scope combinations, repeated item changes, discount caching, and future durability needs.

Related Interview Questions

  • Design a Space-Efficient Versioned Recipe Shopping Cart - Decagon (hard)
|Home/Software Engineering Fundamentals/Decagon

Add Nested Begin, Commit, and Abort Semantics to a Shopping Cart

Decagon logo
Decagon
Jul 13, 2026, 12:00 AM
hardSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
1
0

Add Nested Begin, Commit, and Abort Semantics to a Shopping Cart

An in-memory shopping cart already supports add(item), remove(item), and a pure discount(cart_state) calculation. Extend it with transaction methods:

  • begin() : start a new transaction scope.
  • commit() : close the innermost active scope while preserving its changes.
  • abort() : close the innermost active scope and undo only changes made since its matching begin() .
  • get_total_discount() : calculate from the currently visible state, including uncommitted changes.

Transactions can be nested. For example, after begin(); add(A); begin(); add(B), aborting once removes only B; committing once preserves B inside the still-active outer scope. Aborting when no scope is active is a no-op. State and justify the behavior of commit() with no active scope. Operations performed outside a transaction take effect immediately.

Design the data structures and implement the transaction layer. The item and discount representations may be treated as existing abstractions; focus on state semantics.

Constraints & Assumptions

  • Calls are single-threaded for the base problem.
  • An item may have a quantity greater than one.
  • remove cannot produce a negative quantity; define whether it rejects or clamps an invalid request.
  • The cart can receive many operations between transaction boundaries.

Clarifying Questions to Ask Guidance

  • Does nested commit merge into its parent or make changes irrevocable immediately?
  • Must an invalid remove fail atomically?
  • Is get_total_discount() pure, and may its result be cached?
  • Are transaction scopes required to survive process failure?

What a Strong Answer Covers Guidance

  • Precise nested-scope semantics and visibility
  • A stack-based implementation using snapshots or reversible changes
  • Correct handling of repeated changes to the same item
  • Complexity and memory trade-offs
  • Tests for nested commit/abort combinations and invalid operations

Follow-up Questions Guidance

  • How would you add thread safety?
  • How would the design change if transactions had to survive a crash?
  • Can an undo log avoid copying the full cart at every begin() ?
  • When is cache invalidation for the discount total safe?
Loading comments...

Browse More Questions

More Software Engineering Fundamentals•More Decagon•More Software Engineer•Decagon Software Engineer•Decagon Software Engineering Fundamentals•Software Engineer Software Engineering Fundamentals

Write your answer

Your first approved answer each day earns 20 XP.

Sign in to write your answer.
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.