Data Structures & AlgorithmsHardGATE CS · ICPC regional
A queue is built from two stacks: enqueue pushes onto the inbox stack, and dequeue pops from the outbox stack, first transferring all inbox elements (one element moved per transfer) only when the outbox is empty. Operations in order: enqueue 1, enqueue 2, enqueue 3, dequeue, enqueue 4, dequeue, dequeue. How many element transfers occur in total?
- A. 4
- B. 2
- C. 3
- D. 5
Correct answer: C. 3
The first dequeue moves 3 elements; later transfers happen only when the outbox empties again. Counting one move per enqueue or per dequeue is the usual misreading.
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
- How many non-empty distinct palindromic partitions, i.e. ways to split the string aabb into contiguous pieces where every piece is a…
- How many distinct non-empty subsequences (as strings) does the word aba have?
- A print queue holds jobs with priorities [2, 1, 3, 2] (index 0 to 3). The front job prints only if no waiting job has a higher priority…
- A stack machine runs PUSH 3, PUSH 4, ADD, PUSH 5, MUL in order. The result is the value on top of the stack at the end. What is it?
- How many index pairs i < j in the array [1, 3, 1, 2, 3, 3] have a[i] = a[j]?
- What is the sum of the XOR of every non-empty contiguous subarray of [1, 2, 3]?