Start learning free

LESSON 342 OF 343 · OBJECT-ORIENTED DESIGN - INTERVIEWS

Stock Order Book

Practice O(log n)

This is a design round. You write the classes, and the interviewer checks that they behave.

Here you build the order book of one stock. Traders send orders to buy or sell shares. Each order names a price and a number of shares.

Two classes work together. Order holds one order. It keeps the id, side and price of the order. It also keeps the shares left and the arrival number. OrderBook holds all the orders and matches them. The starter code shows the methods each class needs.

Implement OrderBook:

  • OrderBook() starts with no orders.
  • buy(id, price, quantity) places an order to buy quantity shares at price or less. It returns how many shares it traded right away.
  • sell(id, price, quantity) places an order to sell quantity shares at price or more. It returns how many shares it traded right away.
  • cancel(id) removes the resting order with this id. It returns true if there was one.
  • bestBuy() returns the highest price of a resting buy order, or 0 if there is none.
  • bestSell() returns the lowest price of a resting sell order, or 0 if there is none.
  • left(id) returns how many shares of this order still rest in the book. It returns 0 if the order isn't resting.

An order that waits in the book is resting. Orders arrive one by one, so each has a place in time.

A buy order trades with resting sell orders whose price is at most its own. It takes the lowest price first. Among equal prices, it takes the oldest order first.

Each trade is the smaller of two numbers. One is the shares the buy still wants. The other is the shares that sell order still has. A sell order with no shares left leaves the book.

When no resting sell order fits any more, the rest of the buy stays in the book. It rests as a buy order at its own price. If every share was traded, nothing rests.

A sell order works the same way. It trades with resting buy orders whose price is at least its own. It takes the highest price first, then the oldest order.

A resting order that trades only some of its shares keeps its place in time.

A call to cancel returns false when no order with this id is resting. The id may be unknown. The order may have traded all its shares, or it may be cancelled already.

The price a trade happens at doesn't matter here. Only the number of shares counts.

The calls run in order on one new OrderBook. A call to buy or sell writes the shares traded. A call to cancel writes True or False. The other calls write their number.

 

Example 1:

Input:
OrderBook()
1. sell(1, 101, 5)
2. sell(2, 103, 4)
3. sell(3, 102, 6)
4. bestSell()
5. buy(4, 102, 8)
6. left(3)
7. bestSell()
8. buy(5, 104, 10)
9. bestBuy()
10. left(5)
Output:
0
0
0
101
8
3
102
7
104
3
Explanation:
Calls 1 to 3 find no buy orders, so all three sells rest.
The lowest sell price is 101.
Call 5 wants 8 shares at 102 or less.
It takes 5 shares from order 1, at 101. Order 1 leaves the book.
Then it takes 3 shares from order 3, at 102. Order 3 keeps 3.
Order 2, at 103, costs too much for call 5.
Call 8 takes the 3 shares of order 3 and the 4 of order 2.
Its other 3 shares rest as a buy order at 104.

Example 2:

Input:
OrderBook()
1. buy(10, 50, 4)
2. buy(11, 50, 6)
3. buy(12, 49, 5)
4. sell(13, 50, 3)
5. left(10)
6. sell(14, 48, 4)
7. left(10)
8. left(11)
9. bestBuy()
Output:
0
0
0
3
1
4
0
3
50
Explanation:
Orders 10 and 11 both buy at 50. Order 10 came first.
Call 4 sells 3 shares, all to order 10. Order 10 keeps 1.
Call 6 sells 4 shares at 48 or more.
Order 10 is still ahead of order 11, so it gets 1 share first.
Order 11 gets the other 3 and keeps 3.
Order 12, at 49, gets nothing.

Example 3:

Input:
OrderBook()
1. sell(7, 30, 5)
2. buy(8, 30, 2)
3. left(7)
4. cancel(7)
5. cancel(7)
6. cancel(8)
7. cancel(99)
8. bestSell()
9. buy(9, 35, 1)
10. bestBuy()
Output:
0
2
3
True
False
False
False
0
0
35
Explanation:
Order 8 buys 2 of the 5 shares of order 7. Nothing of order 8 rests.
Call 4 cancels the 3 shares that order 7 still has.
Order 7 is gone now, so call 5 fails.
Order 8 traded all its shares, so call 6 fails.
No order has the id 99, so call 7 fails too.
The sell side is empty, so call 8 returns 0.
Order 9 finds no sell order, so it rests at 35.

 

Constraints:

  • Each id is a whole number from 1 to 1,000,000,000.
  • No two orders have the same id.
  • cancel and left may get any id in that range.
  • Each price is a whole number from 1 to 1,000,000.
  • Each quantity is a whole number from 1 to 10,000.
  • At most 100,000 calls follow the constructor.

Note:

Each heap step should take O(log n) time, for n orders. Keep two heaps of resting orders and a map from ids to orders. A cancelled or fully traded order may stay in its heap. Drop it when it reaches the top. Each order enters a heap once and leaves it once. Scanning every order for the best price would take O(n) per call.

STEP 1 OF 48
    Ctrl + Enter to check