Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon OA

Problem: Maximum profit with at most two buy-sell transactions (one stock, you must sell before buying again) for the daily prices [3, 3, 5, 0, 0, 3, 1, 4]. What is the maximum profit?

  1. A. 6
  2. B. 4
  3. C. 8
  4. D. 2

Correct answer: A. 6

Split the days: best single trade in [3, 3, 5, 0, 0, 3] is 3 (buy 0, sell 3) and in [1, 4] is 3, total 6. One transaction allows only 0 → 4 = 4, unlimited transactions give 2 + 3 + 3 = 8, and stopping after the first rise 3 → 5 gives 2.

More Coding Interview Problems questions

Exams that ask this topic