CCoinSwitch·DSASDE-1DSA Round
Match Buy and Sell Orders from an Order Book
Problem
Given a list of order book entries — each with type (BUY/SELL), price, and quantity — implement an order matching function that returns all matched trades.
Matching rules:
- A BUY order at price P matches the lowest SELL order with price ≤ P
- A SELL order at price P matches the highest BUY order with price ≥ P
- Partial fills are allowed (remaining quantity stays in the book)
Example
orders = [
{type:BUY, price:102, qty:10},
{type:SELL, price:100, qty:8},
{type:SELL, price:101, qty:5},
]
Output: [{buy:102, sell:100, qty:8}, {buy:102, sell:101, qty:2}]
Expected data structures
- BUY side: max-heap by price
- SELL side: min-heap by price
Follow-up
How do you handle orders with the same price? (FIFO queue per price level)
added 6 days ago