ProgrammingVery hardCognizant GenC · TCS NQT
What is printed?
What is printed?
def find_min(a):
lo, hi = 0, len(a) - 1
while lo < hi:
mid = (lo + hi) // 2
if a[mid] > a[hi]:
lo = mid + 1
else:
hi = mid
return a[lo], lo
print(find_min([4, 5, 6, 7, 0, 1, 2]), find_min([3, 1, 2]))- A. (0, 5) (1, 1)
- B. (4, 4) (1, 1)
- C. (0, 4) (1, 1)
- D. (0, 4) (2, 2)
Correct answer: C. (0, 4) (1, 1)
Binary search compares the middle with the right end, which shows which half holds the rotation point. The minimum 0 sits at index 4, and in [3, 1, 2] the minimum 1 sits at index 1.
More Python Programming questions
- What is printed? python g = {'a': [3, 1], 'b': [2]} for v in g.values(): v.sort() print(g, sorted(g.items(), key=lambda kv: len(kv[1])…
- What is printed? python ops = {} for name in ['add', 'mul']: ops[name] = lambda x, y, n=name: x + y if n == 'add' else x y…
- What is printed? python print(list(enumerate('ab', start=1)), list(zip(reversed([1, 2, 3]), 'xyz')))
- What is printed? python print(hash(1) == hash(True), hash(-1) == -2)
- Which statement about dictionary iteration order in Python 3.7 and later is correct?
- What is printed? python class Q: def init(self): self.i, self.o = [], [] def push(self, x): self.i.append(x) def pop(self): if not self.o…