Fix the duplicate charge guard in an SQS consumer
Your task
An SQS queue triggers your Lambda, which charges customers for orders. A standard SQS queue delivers at least once: the same message can arrive twice, for example after a timeout or a retry. Finance found duplicate charges. So the consumer now uses the order id as an idempotency key, a key that makes a repeated request do nothing the second time.
An assistant wrote charge_orders_idempotently(delivered_order_ids). It is in the editor. It has a bug. Find it and fix it.
Input and output:
delivered_order_ids: the order id in each delivery, in delivery order, redeliveries included. Each id is a string. It can have whitespace around it, and it can be empty or all whitespace.- Return a list of strings, one per delivery, in the same order. Each is
"charged","duplicate_skipped"or"invalid_skipped".
Rules the tests check:
- Trim whitespace (spaces, tabs, newlines) at both ends of each id. The trimmed id is the order's key.
- A delivery whose key is empty gets
"invalid_skipped". It does not claim a key. - A delivery whose key was already charged by an earlier delivery gets
"duplicate_skipped". - Any other delivery gets
"charged", and its key counts as charged from then on. - Keys compare exactly. Case matters:
ORD-1andord-1are different orders. - An empty list returns an empty list.
Examples
charge_orders_idempotently(delivered_order_ids: list[str]) -> list[str]Example 1
Input
charge_orders_idempotently(["ord-1", "ord-2", "ord-1"])Output
["charged", "charged", "duplicate_skipped"]Example 2
Input
charge_orders_idempotently(["ord-1", "ord-1", "ord-1"])Output
["charged", "duplicate_skipped", "duplicate_skipped"]Example 3
Input
charge_orders_idempotently([])Output
[]