Data Structures & AlgorithmsHard

Prices over days are 3 3 5 0 0 3 1 4. You may complete at most TWO buy-then-sell transactions, and you must sell before buying again. What is the maximum total profit?

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

Correct answer: A. 6

Split the days: buy 0 sell 3 (profit 3), then buy 1 sell 4 (profit 3), total 6. Unlimited transactions would give 8, stopping after the first sale gives 3, and 2 is only the 3 → 5 rise at the start.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic