ProgrammingVery hardAmazon OA · Flipkart coding round

What is printed?

What is printed?
def two_tx(p):
    b1 = b2 = float('-inf')
    s1 = s2 = 0
    for x in p:
        b1 = max(b1, -x)
        s1 = max(s1, b1 + x)
        b2 = max(b2, s1 - x)
        s2 = max(s2, b2 + x)
    return s2
print(two_tx([3, 3, 5, 0, 0, 3, 1, 4]), two_tx([1, 2, 3, 4, 5]), two_tx([7, 6, 4, 3, 1]))
  1. A. 6 5 0
  2. B. 5 4 0
  3. C. 6 4 1
  4. D. 6 4 0

Correct answer: D. 6 4 0

Four running states track the best value after the first buy, the first sell, the second buy and the second sale. The three price lists give 6, 4 and 0 for at most two trades.

More Python Programming questions

Exams that ask this topic