Crypto Order Lifecycle Status
EasyState Machines · Coinbase
Implement order_statuses(messages, queries). Each message is a comma-separated string with five fields:
text
orderId, side, symbol, quantity, eventTypeFields may have extra spaces around them. side (BUY or SELL) and symbol are informational and do not affect the result. quantity is a positive integer and eventType is NEW or FILLED. Process messages in order:
NEW: creates the order with total quantityquantityand filled quantity0. If the order id already exists, it is replaced (new total, filled reset to0).FILLED: for an existing order, addsquantityto its filled amount, capped at the order's total. A fill for an order id that does not exist yet is ignored.
Order ids are compared as strings ("01" and "1" are different orders). For each id in queries, return its status after all messages:
"UNKNOWN"if the order does not exist,"NEW"if nothing has filled,"COMPLETED"if the filled amount equals the total,"IN_PROGRESS"otherwise.
Example 1
- Input:
- messages = ["1111, BUY, BTC, 10, NEW", "2222, SELL, ETH, 4, NEW", "1111, BUY, BTC, 6, FILLED", "2222, SELL, ETH, 3, FILLED", "2222, SELL, ETH, 3, FILLED"], queries = ["1111", "2222", "3333"]
- Output:
- ["IN_PROGRESS", "COMPLETED", "UNKNOWN"]
- Explanation
- Order 1111 has 6 of 10 filled, so it is in progress. Order 2222 receives fills of 3 and 3 against a total of 4; the filled amount is capped at 4, so it is completed. 3333 never had a
NEWmessage.
Example 2
- Input:
- messages = ["7, BUY, SOL, 5, NEW", "7, BUY, SOL, 5, FILLED", "7, BUY, SOL, 8, NEW"], queries = ["7"]
- Output:
- ["NEW"]
- Explanation
- The second
NEWfor order 7 replaces the order: its total becomes 8 and the filled amount resets to 0, so the status is back toNEW.
Example 3
- Input:
- messages = ["9, SELL, BTC, 2, FILLED", " 9 ,SELL,BTC ,2, NEW "], queries = ["9", "9"]
- Output:
- ["NEW", "NEW"]
- Explanation
- The fill arrives before the order exists, so it is ignored. Spaces around fields are allowed. The same id may be queried more than once.
Constraints
1 <= messages.length <= 10^41 <= queries.length <= 10^41 <= quantity <= 10^9- Every message has exactly five comma-separated fields