ProgrammingHardAmazon OA · Flipkart coding round
What is printed?
What is printed?
def can_jump(a):
far = 0
for i, x in enumerate(a):
if i > far:
return False
far = max(far, i + x)
return True
print(can_jump([2, 3, 1, 1, 4]), can_jump([3, 2, 1, 0, 4]))- A. False True
- B. True True
- C. True False
- D. False False
Correct answer: C. True False
The greedy keeps the farthest reachable index and fails when a position lies beyond it. In [3, 2, 1, 0, 4] the reach stops at index 3, so index 4 cannot be reached.
More Python Programming questions
- What is printed? python import copy a = [[1], [2]] b = a[:] c = copy.deepcopy(a) a[0].append(9) print(b, c)
- What is printed? python def f(x, acc=[]): acc.append(x) return acc f(1) f(2) print(f(3, []), f(4))
- What is printed? python def gridpaths(r, c, memo={}): if r == 0 or c == 0: return 1 if (r, c) not in memo: memo[(r, c)] = gridpaths(r - 1…
- What is printed? python def most2(s): cnt = {} l = best = 0 for r, ch in enumerate(s): cnt[ch] = cnt.get(ch, 0) + 1 while len(cnt) > 2…
- What is printed? python class Bag: items = [] def add(self, x): self.items.append(x) a, b = Bag(), Bag() a.add(1) b.add(2) print(a.items…
- What is printed? python print(max('bca'), min('bca', key=lambda c: (c, -ord(c))))