Data Structures & AlgorithmsModerateGATE CSE
A queue is built from two stacks: enqueue pushes on stack IN; dequeue pops from OUT, first moving all of IN onto OUT (one pop and one push per element) if OUT is empty. Starting empty, the operations enqueue 1, enqueue 2, enqueue 3, dequeue, enqueue 4, dequeue, dequeue, dequeue are performed. How many push operations are done in total on both stacks?
- A. 4
- B. 6
- C. 8
- D. 10
Correct answer: C. 8
Enqueues of 1, 2, 3 and 4 give 4 pushes; moving 1-2-3 to OUT gives 3 pushes and, after OUT empties, moving 4 gives 1 more: 4 + 3 + 1 = 8.
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
- What is the minimum number of adjacent swaps needed to sort [3, 1, 2, 5, 4] in ascending order?
- For each element of [4, 5, 2, 25, 7, 8], take the first strictly greater element to its right (or -1 if there is none). What is the sum of…
- A sliding window of size 3 moves over [1, 3, -1, -3, 5, 3, 6, 7]. What is the sum of the window maximums?
- What is the length of the longest substring of "abcabcbbxyzab" with no repeated character?
- A singly linked list 1 -> 2 -> ... -> 7 is reversed in groups of k = 2; a final group with fewer than k nodes is left as it is. What is…
- The bars of a histogram-shaped terrain have heights [4, 2, 0, 3, 2, 5]. After rain, how many units of water are trapped between the bars?