KatInterview

Cart History: Undo and Redo Item Changes

Data Structure DesignAuto-graded · 22 test cases
MediumAuto-gradedData Structure Designdata-structuresstackstate-management

Last updated

Design a cart with reversible add/remove operations, tracking change history with undo/redo stacks.

Design a cart system that tracks items and supports reversible modifications.

Your cart maintains item identifiers mapped to their counts. Users can increase counts, decrease counts, reverse their last modification, or reapply a modification they just reversed.

Implement a function process_cart_operations(operations, arguments) that executes a sequence of cart commands:

  • "add_item" with arguments [item_id, quantity]: increases the count of the specified item by the given amount. If the item is not yet in the cart, this creates a new entry.
  • "remove_item" with arguments [item_id, quantity]: decreases the count of the specified item. If the count would reach zero, the item is deleted from the cart. If the requested amount exceeds the current count, the operation fails and the cart remains unchanged.
  • "undo" with arguments []: reverses the most recent successful modification (add or remove) that has not already been reversed. If no such modification exists, the operation fails.
  • "redo" with arguments []: reapplies the most recently reversed modification. If no modification has been reversed, or if a new add/remove has occurred since the last undo, the operation fails.
  • "items" with arguments []: returns the current cart contents as a dictionary mapping item IDs to their counts.

The function returns a list of results, one per operation:

  • For "add_item" and "remove_item": True if successful, False if the operation failed
  • For "undo" and "redo": True if successful, False if there was nothing to undo/redo
  • For "items": a dictionary of the current cart state

When a new add or remove operation executes successfully, all previously reversed modifications become invalid and can no longer be redone.

Constraints

  • Item identifiers are strings
  • Quantities are positive integers (≥ 1)
  • 1 ≤ number of operations ≤ 1000
  • The history of modifications is unbounded

Examples

Example 1

Input: process_cart_operations(['add_item', 'add_item', 'items'], [['apple', 2], ['apple', 3], []])

Output: [True, True, {'apple': 5}]

Example 2

Input: process_cart_operations(['add_item', 'remove_item', 'items', 'undo', 'items', 'redo', 'items'], [['book', 4], ['book', 2], [], [], [], [], []])

Output: [True, True, {'book': 2}, True, {'book': 4}, True, {'book': 2}]

Example 3

Input: process_cart_operations(['remove_item', 'add_item', 'undo', 'redo', 'items'], [['missing', 1], ['pen', 1], [], [], []])

Output: [False, True, True, True, {'pen': 1}]

Example 4

Input: process_cart_operations(['add_item', 'add_item', 'undo', 'add_item', 'redo', 'items'], [['a', 1], ['b', 2], [], ['c', 3], [], []])

Output: [True, True, True, True, False, {'a': 1, 'c': 3}]

Hints

Hint 1

Maintain two stacks: one for modifications that can be reversed, and one for modifications that can be reapplied.

Hint 2

Each history entry needs to capture both the previous state and the new state of an item to allow bidirectional navigation.

Hint 3

When a new modification succeeds, clear the stack of reapplicable modifications since those changes are no longer part of the current timeline.