← All Exercises

Apply Requests Exactly Once by Key

intermediatejavascript~25min

Background

Network retries deliver the same payment twice unless the server keys the operation. Exactly-once behaviour is enforced by deduplicating on the client-supplied key.

Problem

Deduplicate a request batch by idempotency key, returning the applied requests, the duplicate keys, and the total charged amount.

Input

JSON object: { requests: [{ key, amount }] }

Output

JSON object: { applied, duplicates: string[], chargedTotal: number }

Sample tests

Sample 1

Input: {"requests":[{"key":"k1","amount":100},{"key":"k2","amount":50},{"key":"k1","amount":100}]}
Expected: {"applied":[{"key":"k1","amount":100},{"key":"k2","amount":50}],"duplicates":["k1"],"chargedTotal":150}

Sample 2

Input: {"requests":[{"key":"k3","amount":20}]}
Expected: {"applied":[{"key":"k3","amount":20}],"duplicates":[],"chargedTotal":20}

Your solution

Learning objective

Make money-moving endpoints safe under retries.