Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: The array [7, 1, 5, 3, 6, 4] is processed to find the maximum profit from one buy and one later sell (0 if none). What is the maximum profit?

  1. A. 6
  2. B. 4
  3. C. 5
  4. D. 7

Correct answer: C. 5

Track the lowest price so far and the best gain. The buy at 1 and sell at 6 gives the largest difference of 5.

More Coding Interview Problems questions

Exams that ask this topic