bugger.
All challenges/Pythonmediumamazon-order-idempotency-v1
THE INVESTIGATION

The checkout key shared by two customers

A checkout retry cache prevents duplicates, but one customer receives another customer’s order.

Amazon-styleInterview practiceIdempotency

What’s going wrong

Original Amazon-style interview practice. This is not an actual Amazon interview question. Bugger does not provide an AI assistant.

Checkout retries should return the original order. After a second customer uses the same client request token, their checkout incorrectly returns the first customer’s order instead of creating their own.

Input is {"requests":[{"customer":"alice","request":"checkout-1","sku":"book","quantity":1}, ...]}. Process requests in array order. The logical identity of an operation is the pair (customer, request). For each first-seen pair create an order with ID "order-1", "order-2", and so on, using the next global creation number. A repeat of the same pair returns the original ID without creating another order. Distinct pairs are independent, even with identical items.

Print {"orders":[...],"responses":[...]}. Orders appear in creation order and include id, customer, request, sku and quantity. Responses contain one order ID per input request. Input contains at most 100 requests. All string fields are nonempty strings of at most 64 ASCII characters; delimiters such as colons are valid. Quantity is an integer from 1 to 100. Repeated pairs always have the same sku and quantity, so payload-conflict handling is outside this exercise. Repair process_requests while preserving retry behavior.

Expected behavior

Create exactly one order per customer/request pair and return its ID for every retry, without sharing results across customers.

Reproduction

01 Two customers reuse a token
{"requests":[{"customer":"alice","request":"checkout-1","sku":"book","quantity":1},{"customer":"bob","request":"checkout-1","sku":"lamp","quantity":2},{"customer":"alice","request":"checkout-1","sku":"book","quantity":1}]}
{"orders":[{"id":"order-1","customer":"alice","request":"checkout-1","sku":"book","quantity":1},{"id":"order-2","customer":"bob","request":"checkout-1","sku":"lamp","quantity":2}],"responses":["order-1","order-2","order-1"]}
02 A retry reuses its order
{"requests":[{"customer":"alice","request":"checkout-1","sku":"book","quantity":1},{"customer":"alice","request":"checkout-1","sku":"book","quantity":1}]}
{"orders":[{"id":"order-1","customer":"alice","request":"checkout-1","sku":"book","quantity":1}],"responses":["order-1","order-1"]}
03 Different intent with identical contents
{"requests":[{"customer":"alice","request":"checkout-1","sku":"book","quantity":1},{"customer":"alice","request":"checkout-2","sku":"book","quantity":1}]}
{"orders":[{"id":"order-1","customer":"alice","request":"checkout-1","sku":"book","quantity":1},{"id":"order-2","customer":"alice","request":"checkout-2","sku":"book","quantity":1}],"responses":["order-1","order-2"]}

Before you begin

  • Dictionaries
  • Composite keys
  • Idempotent operations

Need a nudge?

Reveal one investigation hint at a time. Try each clue before opening the next.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.