Question: Create a MarketPlace class in PYTHON that allows users to buy/sell stocks. It will match buyers with the lowest seller (
Create a MarketPlace class in PYTHON that allows users to buy/sell stocks. It will match buyers with the lowest seller (<= buyer's price) and match sellers with the highest buyer (>= seller's price).
After creating class, the follow up is that input now has expiration dates included in the buy/sell order methods. Revise the class to include handling expiration, and removing from current heaps. Example: (109,Time1), (110,Time2), (110,Time3), (114,Time4)
Hint: use two heaps to maintain order for buys/sells in init method, min heap for sells and max heap for buys
Hint for follow up: change method parameters to accept a currentTime and expirationTime and heaps to consist of a dictionary where the key is the buying/selling price and the value is the currentTime + expirationTime. So when you check for a match the map.key has to match price wise and map.val has to be larger than the current time otherwise the offer is expired.
Example Template:
class Marketplace: def __init__(self, buy_offers, sell_offers):
def match_new_buy_offer(self, amount):
def match_new_sell_offer(self, amount):
Example input:
marketplace = Marketplace(buy_offers=[90, 99, 99, 100, 100], sell_offers=[110, 110, 120]) marketplace.match_new_sell_offer(95) # matches to $100 marketplace.match_new_buy_offer(150) # matches to $110 marketplace.match_new_buy_offer(109) # no match, added to heap marketplace.match_new_sell_offer(100) # matches to $109
Follow up inputs:
marketplace.match_new_buy_offer(price,expiration_time)
marketplace.match_new_sell_offer(price,expiration_time)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
