Got this question from Wish. Anyone knows how to deal with incoming stream of trade orders in a system that can't sort. Is it using PriorityQueue?
Implement a trading system which has the following components:
buy(num_of_product, price)
Called by buyer to buy certain number of products with a maximum price
num_of_product (INTEGER): number of products to buy
price (FLOAT): maximal price/unit the buyer is willing to pay
Return (INTEGER): number of products can be bought
sell(num_of_product, price)
Called by seller to sell certain number of products with a minimum price
num_of_product (INTEGER): number of products to sell
price (FLOAT): minimal price/unit the seller is willing to sell
Return (INTEGER): number of products can be sold
Examples:
system = TradingSystem()
system.sell(50, 1.5)
return: 0
system.sell(20, 1.4)
return: 0
system.buy(60, 1.51)
return: 60
system.buy(20, 1.5)
return: 10
system.sell(20, 0.7)
return: 10
system.buy(100, 0.6)
return: 0