Robinhood 店面

Order Book
Take in a stream of orders (as lists of limit price, quantity, and side, like [“155”, “3”, “buy”]) and return the total number
of executed shares.
Rules

  • An incoming buy is compatible with a standing sell if the buy’s price is >= the sell’s price. Similarly, an incoming sell is
    compatible with a standing buy if the sell’s price <= the buy’s price.
  • An incoming order will execute as many shares as possible against the best compatible order, if there is one (by “best”
    we mean most favorable to the incoming order).
  • Any remaining shares will continue executing against the best compatible order, if there is one. If there are shares of
    the incoming order remaining and no compatible standing orders, the incoming order becomes a standing order.

Example input (more details are in the slide deck):
Stream of orders
(“150”, “10”, “buy”), (“165”, “7”, “sell”), (“168”, “3”, “buy”), (“155”, “5”, “sell”), (“166”, “8”, “buy”)

Example output:
11