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]
- A. 5
- B. 4
- C. 6
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Lomuto partition is applied to a = [3, 8, 2, 5, 1, 4] with the pivot equal to the last element. For j = 0 to n - 2: if a[j] <= pivot, then…
- For the array a = [2, 9, 4, 7, 5, 6, 3] (indices 0 to 6), the procedure below computes the sum of the maximum values of all contiguous…
- Array a = [3, 1, 4, 1, 5, 9, 2, 6] is scanned from left to right. The procedure below counts the positions i (from 2 to n) where a[i] is…
- Array a = [5, 3, 8, 1] (length 4). The procedure below counts how many elements are leaders, where an element is a leader if it is…
- For the array a = [1, 2, 3, 5, 4] the next permutation (in lexicographic order) is computed by the standard procedure: find the last i…
- Array a = [3, 6, -2, -5, 7, 3]. The procedure below finds the largest product of two adjacent elements. What is printed? best = a[0] a[1]…