This question evaluates data-structure design, command parsing, state-management, and error-handling competency for implementing an in-memory order management system, and is categorized under Coding & Algorithms with a practical application focus emphasizing efficient query support.
You are building an in-memory crypto order management system. You must parse commands from standard input and print outputs for query commands.
Each order has:
orderId
(string, unique)
symbol
(string, e.g.,
BTC-USD
)
side
(
BUY
or
SELL
)
qty
(positive integer)
state
∈
{LIVE, PAUSED, CANCELLED}
New orders start in state LIVE.
A sequence of newline-separated commands. Your program should ignore blank lines.
CREATE <orderId> <symbol> <side> <qty>
orderId
already exists, treat it as an error (see
Errors
).
PAUSE <orderId>
LIVE
, change it to
PAUSED
.
RESUME <orderId>
PAUSED
, change it back to
LIVE
.
CANCEL <orderId>
CANCELLED
, change it to
CANCELLED
.
GET <orderId>
<orderId> <state>
.
COUNT <state>
For invalid commands or invalid transitions (e.g., RESUME on a LIVE order, or referencing an unknown orderId), print:
ERROR
(You may assume command tokens are space-separated.)
Print one line per GET and COUNT command, and also one line (ERROR) for each command that errors.
orderId
length ≤ 64.
Design your data structures so that GET and COUNT are efficient, and state transitions do not require scanning all orders.