FastPrepPortfolio Valuation, Part 1
Problems/Portfolio Valuation, Part 1

Portfolio Valuation, Part 1

Stripe logoStripe● EasyNEW GRADPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: In a Stripe new-grad phone screen, the candidate got two inputs: an account with a starting balance and a list of stock trades (symbol, buy or sell, price, quantity and day), and a map of current stock prices. The task was to find the final cash balance, what each stock still held is worth, and the total account value, applying the trades in list order. Only this first part was described.

The report gives no output format and does not say what happens with an oversold stock, a missing price or a stock that was sold out. Here, the input never sells more shares than it holds or spends more than its cash, and every stock still held has a current price. Sold-out stocks are left out, and a stock that has a price but was never held is ignored. The answer is a list of strings: the cash first, then the total, then each stock still held in symbol order. The limits and the other examples are ours. Example 1 uses the two trades and the prices shown in the report; the report's trade list breaks off after those two trades, so its full answer is not known.

The judged task matches the report at about 85%.

An investment account starts with initialBalance in cash and no shares. transactions lists its trades in the order they happened. Each entry is "symbol,type,price,quantity,day":

  • symbol is the stock, and type is buy or sell.
  • price is the price per share for this trade, and quantity is the number of shares.
  • day is the trading day. The list is already in day order, so apply the trades one by one in list order.

A buy pays price * quantity out of cash and adds quantity shares of the stock. A sell removes quantity shares and adds price * quantity to cash.

currentPrices gives today's price of some stocks, each as "symbol,price". After all trades, find:

  • the cash balance;
  • the value of each stock still held, which is the number of shares held times its current price;
  • the total account value, which is the cash plus the values of all stocks held.

Return a list of strings. The first is "cash:<cash>" and the second is "total:<total>". After them comes one "<symbol>:<value>" for each stock with more than zero shares, in increasing string order of the symbol. A stock whose shares were all sold is not listed. All amounts are whole numbers.

Function

valuePortfolio(initialBalance: int, transactions: String[], currentPrices: String[]) → String[]

Examples

Example 1

initialBalance = 200000transactions = ["AAPL,buy,120,20,1","AAPL,sell,130,10,2"]currentPrices = ["AAPL,200","GOOG,150"]return = ["cash:198900","total:200900","AAPL:2000"]
  • Buying 20 shares at 120 costs 2400, so cash goes from 200000 to 197600.
  • Selling 10 shares at 130 brings in 1300, so cash ends at 198900 and 10 AAPL shares are left.
  • At the current price 200 they are worth 2000. GOOG is not held, so its price is not used.
  • The total is 198900 + 2000 = 200900.

Example 2

initialBalance = 10000transactions = ["MSFT,buy,50,40,3","GOOG,buy,100,30,3","MSFT,sell,60,40,4","AMZN,buy,20,100,5","GOOG,sell,90,10,6"]currentPrices = ["AMZN,25","GOOG,110","MSFT,70"]return = ["cash:6300","total:11000","AMZN:2500","GOOG:2200"]
  • Cash: 10000 - 2000 - 3000 + 2400 - 2000 + 900 = 6300.
  • All 40 MSFT shares were sold, so MSFT is not listed.
  • AMZN: 100 shares at 25 are worth 2500. GOOG: 20 shares at 110 are worth 2200.
  • The total is 6300 + 2500 + 2200 = 11000, and the holdings are listed in symbol order: AMZN, then GOOG.

Example 3

initialBalance = 500transactions = []currentPrices = ["AAPL,10"]return = ["cash:500","total:500"]

There are no trades, so the cash stays 500, nothing is held and the total is 500.

Constraints

  • 0 <= initialBalance <= 10^9
  • 0 <= transactions.length <= 1000
  • 1 <= currentPrices.length <= 100
  • Each transactions[i] is "symbol,type,price,quantity,day", where symbol has 1 to 5 uppercase English letters, type is "buy" or "sell", 1 <= price <= 10^4, 1 <= quantity <= 10^4 and 1 <= day <= 365, with numbers written in plain decimal.
  • The days in transactions never decrease.
  • A buy never costs more than the cash held at that moment, and a sell never sells more shares of a symbol than are held at that moment.
  • Each currentPrices[j] is "symbol,price" with the same symbol format and 1 <= price <= 10^4. The symbols in currentPrices are distinct, and every symbol still held after all trades appears there.

More Stripe problems

  • Chat Billing CalculationOA · Seen Nov 2026
  • Deployment Window SchedulerOA · Seen Nov 2026
  • Account Roles With Parent-User InheritancePHONE SCREEN · Seen Nov 2026
All Stripe interview questionsSee Stripe hiring insights
Saved
public String[] valuePortfolio(int initialBalance, String[] transactions, String[] currentPrices) {
    // Write your code here.
}
Checking account…