Generate an Optimal Portfolio Trading Report
Updated
Problem statement
Given an array prices, where prices[i] is the price of one stock on day i, generate a report for an optimal sequence of buy-and-sell transactions.
You may complete as many transactions as you like, but you may hold at most one share at a time. A new share may be bought only after the previous share has been sold. Each reported transaction must have positive profit.
Report the transactions as maximal rising runs. Skip days while the next price is less than or equal to the current price; the resulting day is the buy day. Then continue while the next price is greater than or equal to the current price; the final day of that run is the sell day. Report the transaction only when its sell price is greater than its buy price.
Return one String for each transaction, followed by one total line. A transaction line has exactly the form BUY <buyDay> <buyPrice> SELL <sellDay> <sellPrice> PROFIT <profit>. The final line has exactly the form TOTAL PROFIT <totalProfit>. Day indices are zero-based. If no profitable transaction exists, return only TOTAL PROFIT 0.
Function
portfolioTradingReport(prices: int[]) → String[]Examples
Example 1
prices = [7,1,5,3,6,4]return = ["BUY 1 1 SELL 2 5 PROFIT 4","BUY 3 3 SELL 4 6 PROFIT 3","TOTAL PROFIT 7"]The maximal rising runs are from day 1 at price 1 to day 2 at price 5, and from day 3 at price 3 to day 4 at price 6. Their profits are 4 and 3, for a total profit of 7.
Example 2
prices = [1,2,2,4]return = ["BUY 0 1 SELL 3 4 PROFIT 3","TOTAL PROFIT 3"]The equal prices on days 1 and 2 remain inside one maximal rising run. Buying on day 0 and selling on day 3 earns 3.
Example 3
prices = []return = ["TOTAL PROFIT 0"]There are no days on which to trade, so the report contains only the zero-profit total line.
Constraints
0 <= prices.length <= 30000.0 <= prices[i] <= 10000.- The input always satisfies these bounds, so you do not need to report invalid input.
More Point72 interview questions
- Best Time to Buy and Sell Stock IIPHONE SCREEN · Seen Aug 2026
- Find Minimum in a Rotated Sorted ArrayPHONE SCREEN · Seen Aug 2026
- Maximize Array Beauty After DeletionsOA · Seen Aug 2026
- Simulate a Deterministic Finite AutomatonOA · Seen Aug 2026
- Initial Public OfferingOA · Seen Jul 2026
- Test the HypothesisOA · Seen Jul 2026
- Get Triplet CountOA · Seen Jul 2026
- Internet Service Provider Monthly ReportOA · Seen Jul 2026
- Server SelectionOA · Seen Jul 2026
- Lexicographically Smallest String After Substring OperationOA · Seen May 2026
Similar questions at other companies
- Climb Detection with Recoverable DipsStrava · PHONE SCREEN
- Evenly Schedule Vehicle InspectionsAmazon · PHONE SCREEN
- Experiment Bucket Allocation and RebalancingPinterest · PHONE SCREENPinterest interview questions
- Automation Pipeline, Part 3: Stage DurationPlaid · PHONE SCREENPlaid interview questions
- Future-Aware Cache EvictionBenchling · PHONE SCREENBenchling interview questions
- Conveyor Triplet PackagingGoogle · PHONE SCREENGoogle interview questions
- Maximize Circular Rover TravelBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Priority, Expiration, and LRU EvictionTesla · PHONE SCREENTesla interview questions
- Text JustificationUber · PHONE SCREENUber interview questions
- Find GPU Capacity and Drain a NodeTogether AI · PHONE SCREEN