ProgrammingHardProduct company OA

What is printed?

What is printed?
int a[] = {5, 1, 4, 2, 3};
int *lo = a, *hi = a + 4;
while (lo < hi) {
    if (*lo > *hi) lo++;
    else hi--;
}
printf("%td %d", lo - a, *lo);
  1. A. 4 3
  2. B. 2 4
  3. C. 0 5
  4. D. 1 1

Correct answer: D. 1 1

Each step discards the larger of the two ends (when they are equal it drops the right one), so the smaller value always survives. The pointers meet on the minimum: 1, at index 1. The search is a two-pointer minimum, not a sort.

More C Programming questions

Exams that ask this topic