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
Brief summary: Build a price-time priority limit order book. For each incoming order, repeatedly match against the best-priced opposite orders (best price, then FIFO), emit match lines for fills, and book any remainder. Core data structures: price-sorted maps per side with FIFO queues per price.
The VOprep team has long accompanied candidates through various major company OAs and VOs, including Voleon, Google, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for Tiktok or similar engineering-focused companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.