Design and implement a simplified in-memory stock trading platform where users can buy and sell stocks . The system should support order matching, real-time order book updates, and be extensible to support pricing models, concurrency, and trade history.
🎯 Functional Requirements
Implement the following core functionalities:
1. registerUser(userId, name)
• Registers a new user with a unique ID.
2. placeBuyOrder(userId, stockSymbol, price, quantity, timestamp)
• Places a buy order for a stock at the specified price and quantity.
3. placeSellOrder(userId, stockSymbol, price, quantity, timestamp)
• Places a sell order for a stock at the specified price and quantity.
4. matchOrders(stockSymbol)
• Matches buy and sell orders for the same stock:
• Match highest buy with lowest sell.
• If prices cross, a trade is executed at the sell price.
• Partial fills are supported.
5. getOrderBook(stockSymbol)
• Returns current state of buy/sell order book (sorted).
6. getTradeHistory(stockSymbol)
• Returns executed trades in reverse chronological order.
🧱 Suggested Class Structure
User
• userId
• name
• List<Order> activeOrders
• List<Trade> tradeHistory
Order
• orderId
• userId
• stockSymbol
• OrderType (BUY / SELL)
• price
• quantity
• timestamp
Trade
• tradeId
• buyerId
• sellerId
• stockSymbol
• price
• quantity
• timestamp
OrderBook
• PriorityQueue<Order> buyOrders (sorted by highest price, earliest time)
• PriorityQueue<Order> sellOrders (sorted by lowest price, earliest time)
• List<Trade> tradeHistory
🔁 Order Matching Logic
• On new buy order:
• Check if sell orders exist at equal/lower price.
• If yes → execute trade.
• If not → add to buy order book.
• On new sell order:
• Check if buy orders exist at equal/higher price.
• If yes → execute trade.
• If not → add to sell order book.
🔐 Non-Functional Requirements
• Thread-safety: Ensure safe concurrent access for multiple users placing orders simultaneously.
• Extensibility:
• Easy to add market/limit orders, stop loss, etc.
• Add support for portfolios and P&L later.
• Performance: Efficient matching using priority queues or heaps.
🧪 Sample Test Flow
1 plaintext
2 CopyEdit
3 registerUser ( "u1" , "Alice" )
4 registerUser ( "u2" , "Bob" )
5
6 placeBuyOrder ( "u1" , "AAPL" , 150.0 , 10 , 9 : 30 AM )
7 placeSellOrder ( "u2" , "AAPL" , 148.0 , 5 , 9 : 31 AM )
8
9 → matchOrders ( "AAPL" )
10 → trade executed : 5 shares at $ 148
11
12 → getOrderBook ( "AAPL" ) → u1 has remaining 5 shares to buy
13 → getTradeHistory ( "AAPL" ) → shows one trade
🧠 Design Patterns to Use
• Strategy Pattern for pricing/matching logic
• Observer Pattern for notifying users about matched trades (optional)
• Factory Pattern for creating different order types
✅ Evaluation Criteria
• Correctness of matching engine
• Use of object-oriented principles and clean abstractions
• Thread-safe and extensible design
• Clear trade tracking and book maintenance
• Optional: input validations, logging, and retry logic
✨ Optional Enhancements
• Market vs. Limit orders
• User portfolio tracking
• Trade notifications
• Real-time dashboards or reporting
• RESTful or CLI API simulation