ProgrammingHardAccenture · Cognizant

A rotated sorted array a = [4, 5, 6, 7, 0, 1, 2] has its smallest element at some index. The procedure below finds that index by a linear scan that keeps the index of the minimum seen so far. What is printed?

A rotated sorted array a = [4, 5, 6, 7, 0, 1, 2] has its smallest element at some index. The procedure below finds that index by a linear scan that keeps the index of the minimum seen so far. What is printed?
best = 0
for i = 1 to n - 1
    if a[i] < a[best] then best = i
end for
Print best
  1. A. 3
  2. B. 4
  3. C. 5
  4. D. 6

Correct answer: B. 4

The minimum value 0 sits at index 4, which the scan reaches by replacing the best index only when a strictly smaller value appears. Searching for the maximum gives index 3, reporting a 1-based position gives 5, and the last index is 6.

More Pseudocode and Code Tracing questions

Exams that ask this topic