FastPrepFuture Stock Prices

Future Stock Prices

Hudson River Trading logoHudson River Trading● MediumINTERNOA
Learn

Problem statement

Given an unordered list of future stock prices, what is the maximum amount of profit that you could generate from a starting amount of $1,000.00

Rules:

  • You can trade fractional shares (e.g. if there were shares for $400.00, you could buy/sell 2.5 of them for $1,000.00)
  • All trades occur instantaneously and do not incur any transaction costs.
  • Shares may only be bought/sold on a date that you have a known price.
  • Short selling is not allowed.
  • You do not need to have a position at all times (at any time, if you cannot identify a profitable trade, you do not have to trade).
  • Round final answer to nearest dollar.
  • Do not assume input tuple will be sorted in any manner.
  • Future prices will be given as a list in the following format: [Stock, Date, Price]

Function

calculateMaxProfit(stockPrices: String[]) → int

Complete the function calculateMaxProfit in the editor.

calculateMaxProfit has the following parameter:

  1. String[] stockPrices: an array of stock price information

Returns int: the maximum profit rounded to the nearest dollar

Examples

Example 1

stockPrices = ["CSCO,10/18/2024,41.89", "AMZN,10/10/2024,113.67", "AMZN,10/18/2024,120.5", "CSCO,10/10/2024,43.12"]return = 60

Buy 8.797 AMZN @ $113.67 on 10/10/2024 and sell @ $120.5 on 10/18/2024. Profit = $60

Example 2

stockPrices = ["IBM,12/01/2023,132.05", "IBM,12/03/2023,135.19", "IBM,12/18/2023,134.07", "AAPL,12/01/2023,187.19", "AAPL,12/04/2023,164.33", "AAPL,12/20/2023,180.94", "AAPL,12/21/2023,179.65", "GOOG,12/01/2023,116.41", "GOOG,12/07/2023,111.36", "GOOG,12/19/2023,112.19"]return = 127

Buy 7.573 IBM @ $132.05 on 12/1/2023 and sell @ 135.19 on 12/3/2023. Buy 6.230 AAPL on 12/4/2023 @ $164.33 and sell on 12/20/2023 @ $180.94. Profit = $127

Constraints

  • 🍓🍓

More Hudson River Trading problems

See Hudson River Trading hiring insights
public int calculateMaxProfit(String[] stockPrices) {
  // write your code here
}
stockPrices["CSCO,10/18/2024,41.89", "AMZN,10/10/2024,113.67", "AMZN,10/18/2024,120.5", "CSCO,10/10/2024,43.12"]
expected60
Checking account…