86. Design Stock Market Investing Platform

Design Stock Market Investing Platform
You are asked to design a financial services platform (BuyStocks) where:
  • Users can sign up, buy/sell stocks, and view their portfolios.
  • Admins can add stocks with an initial price for the day and define the list of available stocks.
  • Users start trading with the amount present in their wallet.

Key Definitions

  • User: identified uniquely by userId.
  • Stock: identified uniquely by symbol; has a current price.
  • Wallet: each user has a wallet balance used to buy stocks; selling stocks adds money back to the wallet.
  • Portfolio: a user’s holdings (quantity per stock symbol).
  • Available stocks: the set of stocks added by admins; users can trade only from this set.

Method Signatures

Constructor

BuyStocks()
  • Initializes an empty platform with no users and no stocks.

User Signup

boolean signUp(String userId, String name, int initialWalletAmount)
  • userId is always unique and non blank.
  • 1 < initialWalletAmount < 900,000
  • Returns true if the user is created. otherwise do nothing and return false if called with a duplicate or blank userId.

Admin Stock Management

boolean adminAddOrUpdateStock(String symbol, int price)
  • Adds a new stock to the available list, or updates the price for an existing stock.
  • Returns true if the stock is added/updated; otherwise false.
  • 0 < price < 100,000
  • Symbol contains [A-Z] and - (hyphen)
List<String> listAvailableStocks()
  • Returns the list of available stocks as strings in the format: "SYMBOL PRICE".
  • The returned list must be sorted lexicographically by SYMBOL for deterministic output.

Trading

boolean buyStock(String userId, String symbol, int quantity)
  • 1≤ quantity < 10,000.
  • User must exist and symbol must be in the available stock list.
  • Trade executes at the current stock price.
  • Returns false if the wallet balance is insufficient, or inputs are invalid.
  • Returns true and updates wallet + portfolio on success.
boolean sellStock(String userId, String symbol, int quantity)
  • quantity > 0.
  • User must exist and must own at least quantity shares of symbol.
  • Trade executes at the current stock price.
  • Returns false if holdings are insufficient, or inputs are invalid.
  • Returns true and updates wallet + portfolio on success.

Wallet and Portfolio Views

int getWalletBalance(String userId)
  • Returns the user’s wallet balance.
  • Returns -1 if userId does not exist.
List<String> getPortfolio(String userId)
  • Returns the user’s holdings as strings in the format: "SYMBOL QUANTITY".
  • Include only symbols with QUANTITY > 0.
  • The returned list must be sorted lexicographically by SYMBOL for deterministic output.
  • Returns an empty list if the user exists but holds no stocks.
  • Returns an empty list if userId does not exist.

Constraints

  • 1 <= userId.length() <= 30, 1 <= name.length() <= 60
  • 1 <= symbol.length() <= 10
  • All operations must run in-memory with efficient lookups (e.g., maps/dictionaries).

Notes

  • If a method returns false, the system state must remain unchanged for that operation.
  • All outputs that are lists must be deterministic (sorted as specified).
  • All prices and wallet amounts are integers (no decimals).

Examples

Example 1: Basic signup, buy, sell, and views

  • BuyStocks()(constructor)
  • adminAddOrUpdateStock(symbol="AAPL", price=100)true
  • adminAddOrUpdateStock(symbol="TSLA", price=250)true
  • signUp(userId="u1", name="Asha", initialWalletAmount=1000)true
  • buyStock(userId="u1", symbol="AAPL", quantity=5)true
  • getWalletBalance(userId="u1")500
  • getPortfolio(userId="u1")["AAPL 5"]
  • sellStock(userId="u1", symbol="AAPL", quantity=2)true
  • getWalletBalance(userId="u1")700
  • getPortfolio(userId="u1")["AAPL 3"]
  • listAvailableStocks()["AAPL 100", "TSLA 250"]

Example 2: Invalid operations and deterministic lists

  • BuyStocks()(constructor)
  • adminAddOrUpdateStock(symbol="GOOG", price=300)true
  • adminAddOrUpdateStock(symbol="AMZN", price=200)true
  • listAvailableStocks()["AMZN 200", "GOOG 300"]
  • signUp(userId="u2", name="Ravi", initialWalletAmount=250)true
  • signUp(userId="u2", name="Ravi Again", initialWalletAmount=999)false
  • buyStock(userId="u2", symbol="NFLX", quantity=1)false
  • buyStock(userId="u2", symbol="GOOG", quantity=1)false
  • buyStock(userId="u2", symbol="AMZN", quantity=1)true
  • getWalletBalance(userId="u2")50
  • getPortfolio(userId="u2")["AMZN 1"]
  • sellStock(userId="u2", symbol="AMZN", quantity=2)false
  • sellStock(userId="u2", symbol="AMZN", quantity=1)true
  • getWalletBalance(userId="u2")250
  • getPortfolio(userId="u2")[]
  • getWalletBalance(userId="unknown")-1
  • getPortfolio(userId="unknown")[]




Please use Laptop/Desktop or any other large screen to add/edit code.