Data Structures & AlgorithmsVery hard

A queue holds 1 2 3 4 5 6 7 8 from front to back. Interleave it: take the front element of the first half, then the front of the second half, alternately, so the output is x1, x(n/2+1), x2, x(n/2+2), … Give the output sequence.

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

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

Pair the i-th element of the first half with the i-th element of the second half, emitting the first then the second. Reversing one half or keeping the original order yields a different interleaving.

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

Exams that ask this topic