Design an Online Stock Brokerage
💹 What is an online stock brokerage? It is the app you use to invest. You open an account, move money in, and place orders like "buy 10 shares of AAPL now" or "sell my shares if the price reaches 200". The brokerage sends your orders to the market, keeps track of your cash and the shares you own, and shows you what everything is worth. Watchlists let you follow stocks you don't own yet.
This is a good medium-sized LLD problem because it mixes two kinds of work. Accounts, portfolios and watchlists are straightforward modeling. Orders are where the real design lives: an order can wait for hours, and while it waits, the money or shares it needs must not be spent twice. Most of the interview is about getting that one idea right.
Requirements
"Design an online stock brokerage. Users open accounts, add money, buy and sell stocks, see their portfolio, and keep watchlists."
That covers the features, but not the rules. Ask about the parts that change the code.
Clarifying Questions
You: Do we build the exchange too, the part that matches buyers with sellers?
Interviewer: No. The brokerage sends orders to the market. For this round, assume an order fills completely at the current market price as soon as its price condition is met. Prices come to us from a market data feed.
You: Which order types do we support?
Interviewer: Market orders, which fill right away at the current price, and limit orders. A limit buy fills when the price falls to the limit or below; a limit sell fills when the price rises to the limit or above. Until then, the order stays open, and the user can cancel it.
You: Can a user with 1,000 in cash place two open limit buys of 800 each?
Interviewer: No. If both filled, they would owe money they don't have. An open order must commit the cash or the shares it needs.
You: Can users withdraw cash whenever they like?
Interviewer: Only cash that is not committed to an open order.
You: What should the portfolio show?
Interviewer: The shares they can sell, their free cash, and the total value of the account at current prices. Cost basis and gains are a nice-to-have.
You: And watchlists?
Interviewer: A user can have several named lists, like "tech" or "dividends". A list shows its stocks in the order they were added, each with its current price. Only stocks we have a price for can be added.
You: How do we represent money?
Interviewer: Whole numbers, in cents if you like. No fractional shares.
The third question is the one that shapes the design. Asking it shows the interviewer you spotted the core problem of any system where requests wait: the state at the time of the request is not the state at the time it completes. A brokerage answers this the same way a bank answers card payments or a hotel answers bookings: it puts a hold on what the request needs.
💡 Interview tip: When the interviewer says "assume orders fill at the market price", take it. Building a matching engine inside a brokerage problem is a classic way to run out of time. Mention that the real fill comes from the exchange, and that the order matching engine is its own design.
Final Requirements
- A user can open an account with a unique id, deposit cash, and withdraw cash that is not committed to open orders.
- The market sets the current price of a stock. A stock is listed once it has a price. Orders and watchlists accept only listed stocks.
- A user can place a market order (fills at once at the current price) or a limit order (a buy fills when the price is at or below the limit, a sell when the price is at or above it). Orders always fill completely, at the current price.
- Placing an order holds what it needs: a buy holds
quantity × limit pricefor a limit order, orquantity × current pricefor a market order; a sell holds the shares. If the user does not have enough free cash or free shares, the order is rejected. - Every time a price changes, open limit orders on that stock that the new price triggers are filled, oldest first.
- A filled buy pays the actual cost from its hold and returns the rest to free cash. A filled sell adds the proceeds to free cash.
- A user can cancel their own open order, which releases its hold.
- A user can see their free cash, their free shares of a stock, and the total value of the account: all cash plus all shares at current prices.
- A user can keep several named watchlists, each listing stocks in the order they were added, with current prices.
Out of scope
- Connecting to a real exchange, partial fills and order routing (we come back to this in Extensibility).
- Margin, short selling, options, fractional shares and fees.
- Sign-up, identity checks and bank transfers.
- Price alerts and stop orders (Extensibility).
- Several requests at the same time (Extensibility).