Data Structures & AlgorithmsHardProduct company OA · Infosys pattern

The list 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 is reversed in groups of k = 3 nodes; a final group with fewer than k nodes is left as it is. What is the result?

  1. A. 3 → 2 → 1 → 6 → 5 → 4 → 8 → 7
  2. B. 3 → 2 → 1 → 6 → 5 → 4 → 7 → 8
  3. C. 8 → 7 → 6 → 5 → 4 → 3 → 2 → 1
  4. D. 1 → 2 → 3 → 6 → 5 → 4 → 7 → 8

Correct answer: B. 3 → 2 → 1 → 6 → 5 → 4 → 7 → 8

Groups: (1 2 3) → 3 2 1, (4 5 6) → 6 5 4, and the leftover (7 8) has only 2 nodes, so it stays 7 → 8. '… 8 → 7' is the variant that also reverses a short final group, which the question rules out.

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

Exams that ask this topic