Next

Fix the idempotent charge handler for duplicate requests

Review the AI's codeEasy10 minStripe and Anthropic style

Your task

Your payment API takes an idempotency key with each create-charge request, so a client that retries never pays twice. Twice an agent wrote code that passed the happy path, yet a duplicate request still created a second charge. You model the endpoint over a stream of requests to pin down what it must do.

An assistant wrote resolve_idempotent_charges(request_keys). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • request_keys: the idempotency key of each request, in arrival order.
  • Return, for each request, the id of the charge it gets back, or 0 when it is refused.

Rules the tests check:

  • Trim whitespace from both ends of each key. Keys then compare exactly, so A and a are different keys.
  • A key that is empty after trimming creates no charge and gets 0.
  • The first request with a key creates a new charge. Charge ids are 1, 2, 3 and so on, in the order charges are created.
  • A later request with the same key creates nothing and gets the id of the charge that key created.

Examples

resolve_idempotent_charges(request_keys: list[str]) -> list[int]
Example 1
Inputresolve_idempotent_charges(["a", "b", "a"])
Output[1, 2, 1]
Example 2
Inputresolve_idempotent_charges(["k", "k", "k", "k"])
Output[1, 1, 1, 1]
Example 3
Inputresolve_idempotent_charges([" x ", "x"])
Output[1, 1]