Simulate an Idempotent Refund Service Flow
Problem statement
Process operations for an order service and a refund service:
["CREATE", orderId, amount]creates an order, or reportsEXISTS.["REQUEST", refundId, orderId, amount]creates aPENDINGrefund when enough unreserved amount remains; otherwise it recordsREJECTED. Reusing a refund ID is idempotent and returns its current state.["PROCESS", refundId, "SUCCESS"|"FAIL"]settles or releases a pending reservation. Processing a terminal refund returns its current state.["STATUS", refundId]returns its state orUNKNOWN.["BALANCE", orderId]returnsavailable,refunded, andreserved, orUNKNOWN.
Status lines are exactly ORDER id STATE or REFUND id STATE; balance lines are ORDER id available=A refunded=R reserved=P.
Function
simulateRefunds(operations: String[][]) → String[]Examples
Example 1
operations = [["CREATE","o1","100"],["REQUEST","r1","o1","40"],["BALANCE","o1"],["PROCESS","r1","SUCCESS"],["BALANCE","o1"]]return = ["ORDER o1 CREATED","REFUND r1 PENDING","ORDER o1 available=60 refunded=0 reserved=40","REFUND r1 SUCCEEDED","ORDER o1 available=60 refunded=40 reserved=0"]The pending refund reserves funds and success converts the reservation into refunded value.
Example 2
operations = [["REQUEST","r1","missing","10"],["STATUS","r1"]]return = ["REFUND r1 REJECTED","REFUND r1 REJECTED"]A missing order creates a terminal rejected refund for idempotent retries.
Constraints
1 <= operations.length <= 100000.- IDs are nonempty and contain no spaces.
- CREATE amounts are nonnegative; REQUEST amounts parse as signed integers.
- Every operation has the documented shape.