FastPrepSimulate an Idempotent Refund Service Flow

Simulate an Idempotent Refund Service Flow

DoorDash logoDoorDash● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Process operations for an order service and a refund service:

  • ["CREATE", orderId, amount] creates an order, or reports EXISTS.
  • ["REQUEST", refundId, orderId, amount] creates a PENDING refund when enough unreserved amount remains; otherwise it records REJECTED. 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 or UNKNOWN.
  • ["BALANCE", orderId] returns available, refunded, and reserved, or UNKNOWN.

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.

More DoorDash problems

See DoorDash hiring insights
public String[] simulateRefunds(String[][] operations) {
  // write your code here
}
operations[["CREATE","o1","100"],["REQUEST","r1","o1","40"],["BALANCE","o1"],["PROCESS","r1","SUCCESS"],["BALANCE","o1"]]
expected["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"]
Checking account…