Skip to main content

Idempotent Payment Replays

EasyIdempotencyAPIHash TableBackend Systems

Description

A payments API accepts charge requests that carry an idempotency key, so that a client retry can never double-charge a customer. Requests arrive in order: request i has key keys[i] and amount amounts[i] in cents. The first request with a given key charges its amount. A later request with the same key and the same amount is a replay: it is acknowledged and charges nothing. A later request with the same key but a different amount is a conflict: it is rejected, charges nothing, and the original charge stands. Return the amounts that were actually charged, in the order they were charged.

Examples

Input:keys = ["ord-1","ord-1","ord-2","ord-1"], amounts = [500,500,250,700]
Output:[500,250]
Explanation:

The first ord-1 request charges 500 and ord-2 charges 250. The second ord-1 request repeats the same amount, so it is a replay and nothing is charged. The last ord-1 request asks for 700, which does not match the original 500, so it is a conflict and is rejected.

Input:keys = ["a","b","a","b","c"], amounts = [100,100,100,200,5]
Output:[100,100,5]
Explanation:

Keys a and b are charged on first sight. The second a is a replay of the same amount. The second b asks for 200 instead of 100, so it is rejected as a conflict. Key c is new and is charged its 5 cents.

Input:keys = ["k","k","k"], amounts = [900,901,900]
Output:[900]
Explanation:

Only the first request for key k charges anything. The second request changes the amount, so it is a conflict and is rejected. The third request matches the original 900 again, so it is a harmless replay. One charge of 900 is recorded.

Constraints

  • •1 ≤ keys.length = amounts.length ≤ 10⁴
  • •keys[i] is a non-empty string of at most 32 characters
  • •1 ≤ amounts[i] ≤ 10⁶

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.