As reported: Robinhood virtual onsite coding round (2025), with an inventory-log follow-up. The same question also came with the inventory given as
"SYMBOL/QUANTITY"strings (e.g.["AAPL/50","GOOGL/75"]) and the log entries as"SYMBOL/QUANTITY/SIDE/CONTRA"strings.
Robinhood lets customers buy and sell fractional shares, but exchanges trade whole shares only. Robinhood therefore keeps its own inventory of each symbol to cover the fractions.
For example, with an inventory of 0 AAPL:
- a customer buys
1.5→ Robinhood buys2shares from the exchange and keeps0.5; - another buys
0.4→ filled from inventory,0.1left; - another buys
0.5→0.1is not enough, so Robinhood buys one more share; inventory becomes0.6.
Rules — after every order, a symbol's inventory must be:
- never negative: when a customer buy would make it negative, buy the fewest whole shares from the market to bring it back to
≥ 0(inventory0.2, customer buys0.5→ buy 1 share, inventory0.7); - strictly below one share ("flat"): when customer sells push it to
1or more, sell whole shares to the market until it is below1(sells of0.75and0.50→1.25→ sell 1 share, keep0.25).
Input. All numbers are integers whose last two digits are decimals (1000 = 10.00, 20 = 0.20, 100 = 1).
orders[i] = [symbol, side, quantity, price], all strings.sideis"B"(customer buys) or"S"(customer sells).quantityis a number of shares, or a dollar amount prefixed with$, which is converted to shares atprice(the current price per share).inventory[i] = [symbol, quantity]: the starting inventory.
Process the orders in order and return the updated inventory in the same [symbol, quantity] format.
Example 1
orders = [["AAPL","B","42","100"]]
inventory = [["AAPL","99"]]
Output: [["AAPL","57"]]
0.99 − 0.42 = 0.57.
Example 2
orders = [["AAPL","B","$42","100"]]
inventory = [["AAPL","50"]]
Output: [["AAPL","8"]]
$0.42 at $1.00 per share is 0.42 shares; 0.50 − 0.42 = 0.08.
Example 3
orders = [["AAPL","B","150","100"],["AAPL","B","40","100"],["AAPL","B","50","100"]]
inventory = [["AAPL","0"]]
Output: [["AAPL","60"]]
Example 4
orders = [["AAPL","S","75","100"],["AAPL","S","50","100"]]
inventory = [["AAPL","0"]]
Output: [["AAPL","25"]]
Constraints: 0 ≤ orders.length ≤ 2·10^4; 0 ≤ inventory.length ≤ 10, symbols distinct, each starting quantity in 0 … 99; share quantities 1 … 10^6; dollar amounts 1 … 10^9; prices 1 … 10^5; symbols are uppercase letters. A dollar amount always converts to a whole number of hundredths of a share.
Code runner format: one JSON value per line: orders, inventory. Implement updateInventory(orders, inventory); the harness prints the returned list.
Clarifications
- A symbol that is not in the starting inventory starts at
0. - Return every symbol that appears in the inventory or in an order (even at
0): first the starting inventory's symbols in their order, then new symbols in the order of their first order.
Follow-ups
- Also build an inventory log from the inventory's point of view and return
[current inventory, new order log]. Each log entry is[symbol, quantity, side, contra]. Example: inventory0.00AAPL and a customer buy of0.42log[["AAPL","100","B","MARKET"],["AAPL","42","S","CUSTOMER"]]— the inventory bought 1 share from the market, then sold 0.42 to the customer.