Data Structures & AlgorithmsVery hardTCS NQT · Infosys pattern

Array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is left-rotated by d = 2 positions using the reversal algorithm (reverse first d, reverse rest, reverse whole). What is the result?

  1. A. 9, 10, 1, 2, 3, 4, 5, 6, 7, 8
  2. B. 3, 4, 5, 6, 7, 8, 9, 10, 1, 2
  3. C. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
  4. D. 4, 5, 6, 7, 8, 9, 10, 1, 2, 3

Correct answer: B. 3, 4, 5, 6, 7, 8, 9, 10, 1, 2

Left-rotating by d moves the first d elements to the end in order: reversing the first d elements, then the remaining n−d, then the whole array achieves this in O(n) time with O(1) extra space, giving [3, 4, 5, 6, 7, 8, 9, 10, 1, 2]. Rotating right by d instead would give [9, 10, 1, 2, 3, 4, 5, 6, 7, 8].

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic