If Robinhood has 0 shares of AAPL and then a customer wishes to purchase 1.5 AAPL shares, Robinhood will need to request 2 shares from the exchange and hold on to the remaining 0.5 shares.
If another customer requests to purchase 0.4 shares of AAPL, Robinhood can use its inventory (0.5 shares) instead of going out to the exchange and will have 0.1 shares of AAPL remaining.
If the third customer requests 0.5 shares, Robinhood can fill 0.1 shares out of inventory but will need to go to the exchange for an additional share leaving Robinhood’s inventory at 0.6 shares.
If a customer requests a dollar based order, we need to convert it to the relevant number of shares and run through the above steps.
Always ensure the firm has a positive quantity in inventory and has under one share after handling an order. There’s no need for us to hold onto whole shares!
Steps:
Handle buying fractional shares.
Handle selling fractional shares.
Ensure inventory is less than 1 after each order.
e.g. Customer sells AAPL for 0.75 and then another sells AAPL for 0.50 – we have 1.25 inventory. We can sell 1 share to the market and keep our inventory small at 0.25.
Ensure inventory is always non-negative after each order.
e.g. Inventory is 0.2 and the customer buys 0.5 shares: ensure we end up with 0.7 shares in inventory.
Always “flatten”! (steps 3+4)
The final 2 digits of every integer is the decimal. e.g. 1000 = 10.00, 20 = 0.20, 100 = 1.
Example scenario:
Input:
// One AAPL buy order for 0.42 shares. AAPL is currently worth $1.
orders: ["AAPL/B/42/100"]
// Inventory for AAPL is currently 0.99 shares.
inventory: ["AAPL/99"]
Expected Output:
// The users buys 0.42 shares from inventory, leaving us with 0.57 shares.
["AAPL/57"]
Another example scenario:
Input:
// One AAPL buy order for 1, so that's 0.42 shares.
orders: ["AAPL/B/$42/100"]
// Existing AAPL inventory is 0.50 shares.
inventory: ["AAPL/50"]
Expected Output:
// 0.50 - 0.42 = 0.08 shares leftover.
["AAPL/8"]
Input Format
orders Array of strings, each string would be in the format of $SYMBOL/$BUY_OR_SELL/$QUANTITY/$CURRENT_PRICE
$SYMBOL: Can be “AAPL”, “GOOGL”, “MEOOOOOW” or anything really.
$BUY_OR_SELL: “B” or “S”. B for BUY, S for SELL.
$QUANTITY: Can be a number or a dollar amount (prefixed with $). e.g. “100” for 1 quantity or “$150” for $1.50.
$CURRENT_PRICE: Current price of the symbol with no $ sign. e.g. “1000” for $10.
inventory array of strings, each string would be in the foramt of $SYMBOL/$QUANTITYAn example for AAPL of 0.50 shares and GOOGL of 0.75 shares would be: ["AAPL/50", "GOOGL/75"]
Output Format
Output the final inventory after executing all orders. This is expected to be in the same order and format as the inventory input, e.g.: ["AAPL/50", "GOOGL/75"]
Follow-up
Build an inventory log for all orders from the perspective of the Inventory. The new output would be an array of two lists where the first value is the current inventory list and the second value is the new list of order logs.
The inventory log is an ordered list of type [symbol, quantity, side, contra] from the perspective of the inventory. For example:
[“AAPL/100/B/MARKET”], [“AAPL/42/S/CUSTOMER”]
This list means that we first bought 1 share of AAPL from the market, then sold 0.42 shares of AAPL to the customer.
[], [<new_order_log_list>]