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.
- A. 1 5 2 6 3 7 4 8
- B. 8 7 6 5 4 3 2 1
- C. 1 2 3 4 5 6 7 8
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Starting from an empty stack, perform in order: push 4, push 9, pop, push 2, push 7, pop, pop, push 5. Reading from the bottom to the top…
- Take the values of 4 9 2 7 3 8 5 6 1 10 at the ODD 0-based indices (1, 3, 5, …) in order, forming a new sequence. What is the length of…
- Split 8 3 5 1 9 2 7 4 into consecutive blocks of 4 (positions 0–3 and 4–7). For each block take (max − min), and add these two numbers…
- In 3 1 4 1 5 9 2 6 5 3 5 8, count how many positions hold a NEW strict prefix maximum (the element is strictly greater than every element…
- How many DISTINCT strings are produced by all cyclic rotations of "abcabc" (including the identity rotation)?
- How many inversions (pairs i < j with A[i] > A[j]) does 4 5 6 1 2 3 contain?