Implement an in-memory limit order book for a single trading symbol. You do not need to implement order matching/execution—only store and maintain orders and provide book queries.
The order book has two sides:
Within the same price level, orders follow FIFO (earlier timestamp first).
Each order has:
order_id
(unique string or integer)
side
∈
{BUY, SELL}
price
(positive integer)
qty
(positive integer)
ts
(monotonic integer timestamp assigned at insertion; used for FIFO)
Design and implement a class (or module) with the following operations:
add(order_id, side, price, qty)
order_id
does not already exist.
cancel(order_id)
modify(order_id, new_qty)
new_qty == 0
, treat it as
cancel(order_id)
.
best_bid()
and
best_ask()
(price, total_qty)
.
None
.
top_of_book()
None
for a missing side).
M
is the number of distinct price levels on a side.