ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def peak(a):
    lo, hi = 0, len(a) - 1
    while lo < hi:
        mid = (lo + hi) // 2
        if a[mid] < a[mid + 1]:
            lo = mid + 1
        else:
            hi = mid
    return lo
print(peak([1, 3, 8, 12, 4, 2]), peak([10, 9, 8]))
  1. A. 2 0
  2. B. 3 0
  3. C. 3 2
  4. D. 4 0

Correct answer: B. 3 0

The comparison with the next item shows whether the peak lies to the right. 12 is the peak at index 3 of the first list, and a strictly falling list peaks at index 0.

More Python Programming questions

Exams that ask this topic