ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode (finding the minimum in a rotated sorted array via binary search)? (Arrays are indexed from 0. All variables are integers, so / is integer division.)

What will be the output of the following pseudocode (finding the minimum in a rotated sorted array via binary search)? (Arrays are indexed from 0. All variables are integers, so / is integer division.)
Integer arr[7] = {4, 5, 6, 7, 0, 1, 2}
Integer lo = 0, hi = 6
while (lo < hi)
    Integer mid = (lo + hi) / 2
    if (arr[mid] > arr[hi])
        lo = mid + 1
    else
        hi = mid
    end if
end while
Print arr[lo]
  1. A. 4
  2. B. 1
  3. C. 0
  4. D. 2

Correct answer: C. 0

This binary search exploits the rotation point: comparing the middle to the rightmost element tells which half is sorted, narrowing in on the true minimum, which is 0 (located at index 4).

More Pseudocode and Code Tracing questions

Exams that ask this topic