ProgrammingVery hardAmazon OA · Flipkart coding round
What is printed?
What is printed?
def max_prod(a):
best = cur_max = cur_min = a[0]
for x in a[1:]:
cands = (x, cur_max * x, cur_min * x)
cur_max, cur_min = max(cands), min(cands)
best = max(best, cur_max)
return best
print(max_prod([2, 3, -2, 4]), max_prod([-2, 3, -4]))- A. 6 6
- B. 6 24
- C. 4 24
- D. 6 -4
Correct answer: B. 6 24
The code tracks both the running maximum and minimum, since a negative times a negative can become the new maximum. For [-2, 3, -4] the best product is (-2) x 3 x (-4) = 24.
More Python Programming questions
- What is printed? python def jumps(a): j = cur = far = 0 for i in range(len(a) - 1): far = max(far, i + a[i]) if i == cur: j += 1 cur = far…
- What is printed? python def profit(p): lo, best = float('inf'), 0 for x in p: lo = min(lo, x) best = max(best, x - lo) return best…
- What is the time complexity of building a binary heap from n unordered items with heapq.heapify?
- Which statement about bisect.insort(a, x) on a plain Python list a is true?
- What is printed? python import itertools print(list(itertools.accumulate([3, 1, 4], initial=10)))
- What is printed? python from itertools import groupby s = 'aabbaa' print([(k, len(list(g))) for k, g in groupby(s)])