ProgrammingHardAmazon OA · Flipkart coding round
What is printed?
What is printed?
def candy(r):
n = len(r)
c = [1] * n
for i in range(1, n):
if r[i] > r[i - 1]:
c[i] = c[i - 1] + 1
for i in range(n - 2, -1, -1):
if r[i] > r[i + 1]:
c[i] = max(c[i], c[i + 1] + 1)
return sum(c)
print(candy([1, 0, 2]), candy([1, 2, 2]))- A. 4 4
- B. 5 5
- C. 6 4
- D. 5 4
Correct answer: D. 5 4
Two passes give each child at least one more sweet than a lower-rated neighbour on each side. The first case needs 2, 1, 2 for a total of 5, and the second needs 1, 2, 1 for a total of 4.
More Python Programming questions
- What is printed? python def mat(a, b, m): return [[sum(a[i][k] b[k][j] for k in range(2)) % m for j in range(2)] for i in range(2)] def…
- What is printed? python class A: x = 1 def get(self): return self.x a = A() a.x = 5 A.x = 9 b = A() print(a.get(), b.get(), A.x)
- What is printed? python class A: def f(self): return ['A'] class B(A): def f(self): return ['B'] + super().f() class C(A): def f(self)…
- What is printed? python from itertools import product print(len(list(product('01', repeat=3))), list(product('ab', 'x'))[-1], sum(1 for in…
- What is printed? python import heapq, itertools c = itertools.count() h = [] for p, name in [(2, 'x'), (1, 'y'), (2, 'z'), (1, 'w')]…
- What is printed? python d = dict.fromkeys(['a', 'b'], []) d['a'].append(1) print(d, d['b'] is d['a'])