ProgrammingHardAmazon OA · Cognizant GenC Next

What will be the output of the following pseudocode? (Arrays are indexed from 0)

What will be the output of the following pseudocode? (Arrays are indexed from 0)
Integer arr[7] = {1, 2, 3, 4, 5, 6, 7}
reverse(arr, 0, 2)      // reverses arr[0..2]
reverse(arr, 3, 6)
reverse(arr, 0, 6)
Print arr[0], arr[3], arr[6]
  1. A. 4 7 3
  2. B. 5 1 4
  3. C. 7 4 1
  4. D. 3 6 2

Correct answer: A. 4 7 3

Reversing the first 3, then the last 4, then the whole array is the reversal trick for a LEFT rotation by 3: {4, 5, 6, 7, 1, 2, 3}. So arr[0], arr[3], arr[6] = 4 7 3. 5 1 4 is a right rotation by 3.

More Pseudocode and Code Tracing questions

Exams that ask this topic