You have been tasked with creating an exchange where traders can buy and sell Sea Shells. The program reads incoming orders and emits trades.
Input (stdin) – the input is a stream of limit orders. Each line is a separate order identified by its line number (starting at 1), with space-delimited fields:
- the string
"limit" - side:
"buy"or"sell" - volume (integer): positive number of Sea Shells to trade
- price (decimal): a positive price limit with two decimals (e.g., 99.50)
Examples:
limit buy 10 55.30
limit sell 5 57.12
Processing orders
- Incoming limit orders are matched against previously booked orders of the opposite side. Each match emits a trade.
- Matching continues until the incoming order is completely filled or there are no further matches. Any unfilled remainder of the incoming order is booked for future matching.
- Orders are processed in arrival order; order k must finish executing before k+1 executes.
Matching rules
- Prices must be compatible:
- A sell may match highest priced buys first, at or above the sell’s price.
- A buy may match lowest priced sells first, at or below the buy’s price.
- Tie-breakers: if multiple resting orders have the same best price, match the oldest first (FIFO at each price level).
Executing trades / Output (stdout)
For each match, output a line:
match <incoming_order_id> <resting_order_id> <traded_volume> <price>
Example
Input:
limit buy 10 99.00
limit buy 15 100.00
limit buy 3 100.50
limit sell 5 100.00
limit buy 5 99.50
Output:
match 4 3 3 100.50
match 4 2 2 100.00
总结 :实现一个 撮合引擎 :顺序读取限价单,按价格优先、同价 FIFO 的规则与对手方订单簿撮合,撮合完成输出 match incoming_id resting_id volume price;若未完全成交,将剩余量挂入订单簿。买单先吃 最低卖价 ,卖单先吃 最高买价。考点:堆 / 有序映射 + 队列管理、价格比较、部分成交与剩余挂单。
VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 Voleon、Google、Amazon、Citadel、SIG 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备 Tiktok 或类似工程向公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。