Maintain Fractional Stock Inventory Across Orders
RobinhoodSoftware Engineer · OnsiteLast updated
Simulate sequential customer fractional buy and sell orders, using scaled integer arithmetic to maintain the firm's remaining inventory between 0 and 1 share while tracking whole-share market executions.
Problem Statement
Brokerages that offer fractional share trading often act as principal to fulfill customer orders. When a customer buys fractional shares, the brokerage provides them out of its own inventory; when a customer sells, the brokerage absorbs those shares into its inventory.
To manage market risk, the brokerage enforces a strict inventory rule: after every order, the firm's inventory for each stock must be strictly within the range [0, 1.0) shares (i.e., at least 0 shares, but strictly less than 1 whole share). Whenever inventory falls below 0 or reaches 1.0 or more, the brokerage immediately executes an external market order for whole shares to rebalance.
To prevent floating-point precision issues, all share quantities are tracked in micro-shares, where:
1 whole share = 1,000,000 micro-shares (SCALE = 10^6).
Given a list of customer orders, simulate the inventory changes sequentially and return the final state for every symbol.
Order Rules
For each order:
- Convert the dollar amount to customer micro-shares using integer division:
customer_shares = (dollar_amount * 1_000_000) // price - Update firm inventory based on customer action:
- Customer
"BUY": customer takes shares from the firm (inventory -= customer_shares). - Customer
"SELL": customer delivers shares to the firm (inventory += customer_shares).
- Customer
- Rebalance firm inventory to satisfy
0 = 1_000_000: firm executes an external market SELL forinventory // 1_000_000whole shares, removing them from inventory.
Each symbol starts with 0 inventory and 0 external market trades.
Function Signature
def simulate_fractional_trading(orders: list[dict]) -> list[dict]:
pass
Each input order is a dictionary:
"symbol"(str): Stock ticker symbol."side"(str):"BUY"or"SELL"."dollar_amount"(int): Integer dollar amount."price"(int): Price per share in dollars.
Return a list of dictionaries, sorted alphabetically by symbol, containing:
"symbol"(str)"inventory"(int): Final inventory in micro-shares (0 <= inventory < 1_000_000)."market_buys"(int): Cumulative whole shares bought on the external market."market_sells"(int): Cumulative whole shares sold on the external market.
Only include symbols that appeared in at least one order.
Examples
Example 1
orders = [
{"symbol": "AAPL", "side": "BUY", "dollar_amount": 150, "price": 100},
{"symbol": "AAPL", "side": "SELL", "dollar_amount": 70, "price": 100}
]
simulate_fractional_trading(orders)
# Output:
# [
# {"symbol": "AAPL", "inventory": 200000, "market_buys": 2, "market_sells": 1}
# ]
Explanation:
- Order 1: AAPL price = 100, amount = 150. Shares =
(150 * 10^6) // 100 = 1_500_000. Customer buys, so inventory drops from 0 to-1_500_000. Firm buysceil(1_500_000 / 10^6) = 2whole shares from market. Inventory becomes500_000micro-shares.market_buys = 2. - Order 2: AAPL price = 100, amount = 70. Shares =
700_000. Customer sells, so inventory becomes500_000 + 700_000 = 1_200_000. Firm sells1_200_000 // 10^6 = 1whole share to market. Inventory becomes200_000.market_sells = 1.
Constraints
1 <= len(orders) <= 10^51 <= dollar_amount <= 10^81 <= price <= 10^8sideis always"BUY"or"SELL"symbolconsists of 1 to 5 uppercase ASCII letters.
Hints
Hint 1
Use integer arithmetic throughout: compute customer micro-shares as `(dollar_amount * 1_000_000) // price` to prevent floating-point drift.
Hint 2
A customer BUY decreases the firm's inventory, whereas a customer SELL increases it.
Hint 3
When `inventory < 0`, the number of whole shares to buy from the market is `(-inventory + 999_999) // 1_000_000`.
Follow-up questions
- How would you extend this logic to track the firm's cost basis and realized PnL across whole-share market executions and fractional customer executions?
- If multiple threads process orders concurrently for different symbols versus the same symbol, how would you design the synchronization mechanism?
- How would the rebalancing strategy change if the external market only accepts trades in standard round lots of 100 shares rather than 1 whole share?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.