ProgrammingHardAccenture · Cognizant

Array a = [1, 2, 3, 4, 5, 6, 7] is rotated to the right by k = 3 positions: each element moves k places to the right with wrap-around. The procedure below performs the rotation by reversing parts of the array (reverse all, then reverse the first k, then the rest). What is the first element of the result printed?

Array a = [1, 2, 3, 4, 5, 6, 7] is rotated to the right by k = 3 positions: each element moves k places to the right with wrap-around. The procedure below performs the rotation by reversing parts of the array (reverse all, then reverse the first k, then the rest). What is the first element of the result printed?
reverse whole array
reverse a[0..k-1]
reverse a[k..n-1]
Print a[0]
  1. A. 5
  2. B. 4
  3. C. 6
  4. D. 3

Correct answer: A. 5

Rotating right by 3 moves 5, 6, 7 to the front, so the first element is 5. Reading index 3 gives 4 (a left-style read), rotating by 2 instead gives 6, and reading index k-1 gives 3.

More Pseudocode and Code Tracing questions

Exams that ask this topic