Maximum Profit with Exactly Two Stock Transactions
Updated
Problem statement
Given daily stock prices, complete exactly two non-overlapping transactions. Each transaction buys once and sells on a later day, and the first sale must occur before the second purchase.
Return the maximum sum of the two profits. Both transactions are required, so either one may lose money.
Function
maxProfitExactlyTwo(prices: int[]) → intExamples
Example 1
prices = [3,3,5,0,0,3,1,4]return = 6Trade from 3 to 5, then from 0 to 4.
Example 2
prices = [7,6,4,3,1]return = -2Two required adjacent one-point losses are optimal.
Example 3
prices = [1,2,3,4]return = 2Use two one-point gains: 1 to 2 and 3 to 4.
Constraints
4 <= prices.length <= 100000.0 <= prices[i] <= 1000000.- The answer fits in a signed 32-bit integer.
More Retool interview questions
- Maximum Profit with Exactly One Stock TransactionPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Order Sources by Their DependenciesPHONE SCREEN · Seen Oct 2026
- Execute In-Memory SQL QueriesPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2026
- Next Consistent Wordle GuessPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2024
- Wordle FeedbackPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2024
- Wordle Guess LoopPHONE SCREEN · Seen Jun 2024
- Markov Transition Counts and ProbabilitiesONSITE INTERVIEW · Seen Mar 2026
Similar questions at other companies
- Maximal SquareAmazon · ONSITE INTERVIEW
- Maximum Subarray Sum with One DeletionServiceNow · PHONE SCREENServiceNow interview questions
- Distinct Subset SumsGoogle · ONSITE INTERVIEWGoogle interview questions
- Maximum Product of a Contiguous SubarrayLinkedIn · PHONE SCREENLinkedIn interview questions
- Count Subsets With a Target SumSafe Security · ONSITE INTERVIEW
- Fewest Coins CombinationBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Longest Increasing SubsequenceByteDance · PHONE SCREENByteDance interview questions
- Jump GamePaytm · PHONE SCREENPaytm interview questions
- Maximum Subarray Sum with One DeletionOracle · ONSITE INTERVIEWOracle interview questions
- Query Words Within K PositionsPalantir · PHONE SCREENPalantir interview questions