Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern

The first k = 4 elements of the queue 1, 2, 3, 4, 5, 6 (front first) are reversed using a stack: dequeue 4 items onto the stack, pop them back into the queue, then move the remaining n − k items from front to back. What is the queue now, front first?

  1. A. 5, 6, 4, 3, 2, 1
  2. B. 1, 2, 3, 4, 6, 5
  3. C. 4, 3, 2, 1, 5, 6
  4. D. 6, 5, 4, 3, 2, 1

Correct answer: C. 4, 3, 2, 1, 5, 6

After the stack step the queue is 5, 6, 4, 3, 2, 1 (the reversed block went to the back). Rotating the 2 untouched items 5, 6 from front to back gives 4, 3, 2, 1, 5, 6. Forgetting the final rotation leaves 5, 6, 4, 3, 2, 1.

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

Exams that ask this topic