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]))- A. 6 5 0
- B. 5 4 0
- C. 6 4 1
- 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
- What is printed? python def tile(n): dp = [0] (n + 1) dp[0] = 1 for i in range(1, n + 1): dp[i] = dp[i - 1] + (dp[i - 2] if i >= 2 else 0)…
- A program sorts the whole list again for each of 10^5 queries on a list of 10^5 items. Which statement about running it in CPython is…
- What is printed? python from collections import Counter def mw(s, t): need = Counter(t) have = {} formed = 0 req = len(need) l = 0 best =…
- What is printed? python import bisect board = [] for name, score in [('a', 5), ('b', 9), ('c', 5)]: bisect.insort(board, (-score, name))…
- What is printed? python print(sum([1, 2], 0.5))
- What is printed? python import heapq def topo(n, edges): indeg = [0] n adj = [[] for in range(n)] for u, v in edges: adj[u].append(v)…